Skip to content
New issue

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

If-statement type guards #53718

Closed
5 tasks done
oliveryasuna opened this issue Apr 10, 2023 · 3 comments
Closed
5 tasks done

If-statement type guards #53718

oliveryasuna opened this issue Apr 10, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@oliveryasuna
Copy link

Suggestion

πŸ” Search Terms

  • type guard
  • type-guard
  • type guards
  • type-guards
  • type predicate
  • type-predicate
  • type predicates
  • type-predicates

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Support for type predicates and potentially other type guards on if-statements.

πŸ“ƒ Motivating Example

Consider the following use of the typeof type guard.

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?

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:

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.
}

πŸ’» Use Cases

See above.

@xiBread
Copy link

xiBread commented Apr 10, 2023

Duplicate of #10421

@fatcerberus
Copy link

#10421 appears to be the seminal issue that eventually gave us type predicates; a more direct duplicate would be #44192

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 10, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants