We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
My suggestion meets these guidelines:
Support for type predicates and potentially other type guards on if-statements.
Consider the following use of the typeof type guard.
typeof
if(typeof myVar === 'string') { // It's a string. } else if(typeof myVar === 'number') { // It's a number. }
What if I want to store the typeof myVar in a variable?
myVar
const type = typeof myVar; if(type === 'string') { // No type guard. } else if(type === 'number') { // No type guard. }
That example itself, poses a question that could spark another suggestion: Why not support type guards with such a syntax?
My suggestion offers more granularity. Consider the following proposed syntax:
const type = typeof myVar; if(type === 'string'): myVar is string { // It's a string. } else if(type === 'number'): myVar is number { // It's a number. }
Here's a better example, wherein one property can be used to identity
enum Sides { THREE, FOUR } interface Shape { readonly sides: Sides; } class Triangle implements Shape { public readonly sides: Sides = Sides.THREE; } class Rectangle implements Shape { public readonly sides: Sides = Sides.FOUR; } const shape: Shape = ...; if(shape.sides === Sides.THREE): shape is Triangle { // It's a Triangle. } else if(shape.sides === Sides.FOUR): shape is Rectangle { // It's a Rectangle. }
This could also be done with the in operator:
in
enum Sides { THREE, FOUR } interface Shape { readonly sides: Sides; } class Triangle implements Shape { public readonly sides: Sides = Sides.THREE; public someTriangleProperty: any; } class Rectangle implements Shape { public readonly sides: Sides = Sides.FOUR; public someRectangleProperty: any; } const shape: Shape = ...; if(shape.sides === Sides.THREE): 'someTriangleProperty' in shape { // It's has someTriangleProperty. } else if(shape.sides === Sides.FOUR): shape is Rectangle { // It's a Rectangle. }
See above.
The text was updated successfully, but these errors were encountered:
Duplicate of #10421
Sorry, something went wrong.
#10421 appears to be the seminal issue that eventually gave us type predicates; a more direct duplicate would be #44192
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
No branches or pull requests
Suggestion
π Search Terms
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Support for type predicates and potentially other type guards on if-statements.
π Motivating Example
Consider the following use of the
typeof
type guard.What if I want to store the typeof
myVar
in a variable?That example itself, poses a question that could spark another suggestion:
Why not support type guards with such a syntax?
My suggestion offers more granularity.
Consider the following proposed syntax:
Here's a better example, wherein one property can be used to identity
This could also be done with the
in
operator:π» Use Cases
See above.
The text was updated successfully, but these errors were encountered: