-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
This type predicates for type guards #5906
Changes from all commits
c4cff98
859cc53
d7208e8
cfe2cc3
d51dd84
b9f310d
be6e341
b6eb4ae
4594301
0228ec3
b0bfa0f
dc765b8
0284846
8238656
bc01b16
2885eb2
8e58694
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -733,11 +733,15 @@ namespace ts { | |
// @kind(SyntaxKind.StringKeyword) | ||
// @kind(SyntaxKind.SymbolKeyword) | ||
// @kind(SyntaxKind.VoidKeyword) | ||
// @kind(SyntaxKind.ThisType) | ||
export interface TypeNode extends Node { | ||
_typeNodeBrand: any; | ||
} | ||
|
||
// @kind(SyntaxKind.ThisType) | ||
export interface ThisTypeNode extends TypeNode { | ||
_thisTypeNodeBrand: any; | ||
} | ||
|
||
export interface FunctionOrConstructorTypeNode extends TypeNode, SignatureDeclaration { | ||
_functionOrConstructorTypeNodeBrand: any; | ||
} | ||
|
@@ -756,7 +760,7 @@ namespace ts { | |
|
||
// @kind(SyntaxKind.TypePredicate) | ||
export interface TypePredicateNode extends TypeNode { | ||
parameterName: Identifier; | ||
parameterName: Identifier | ThisTypeNode; | ||
type: TypeNode; | ||
} | ||
|
||
|
@@ -1820,10 +1824,25 @@ namespace ts { | |
CannotBeNamed | ||
} | ||
|
||
export const enum TypePredicateKind { | ||
This, | ||
Identifier | ||
} | ||
|
||
export interface TypePredicate { | ||
kind: TypePredicateKind; | ||
type: Type; | ||
} | ||
|
||
// @kind (TypePredicateKind.This) | ||
export interface ThisTypePredicate extends TypePredicate { | ||
_thisTypePredicateBrand: any; | ||
} | ||
|
||
// @kind (TypePredicateKind.Identifier) | ||
export interface IdentifierTypePredicate extends TypePredicate { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like overkill to have two different kinds of TypePredicate types and a dedicated enum. Could you just use |
||
parameterName: string; | ||
parameterIndex: number; | ||
type: Type; | ||
} | ||
|
||
/* @internal */ | ||
|
@@ -2091,6 +2110,7 @@ namespace ts { | |
ESSymbol = 0x01000000, // Type of symbol primitive introduced in ES6 | ||
ThisType = 0x02000000, // This type | ||
ObjectLiteralPatternWithComputedProperties = 0x04000000, // Object literal type implied by binding pattern has computed properties | ||
PredicateType = 0x08000000, // Predicate types are also Boolean types, but should not be considered Intrinsics - there's no way to capture this with flags | ||
|
||
/* @internal */ | ||
Intrinsic = Any | String | Number | Boolean | ESSymbol | Void | Undefined | Null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More than anything else, can you leave a comment about the interaction here with |
||
|
@@ -2102,7 +2122,7 @@ namespace ts { | |
UnionOrIntersection = Union | Intersection, | ||
StructuredType = ObjectType | Union | Intersection, | ||
/* @internal */ | ||
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral, | ||
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral | PredicateType, | ||
/* @internal */ | ||
PropagatingFlags = ContainsUndefinedOrNull | ContainsObjectLiteral | ContainsAnyFunctionType | ||
} | ||
|
@@ -2123,6 +2143,11 @@ namespace ts { | |
intrinsicName: string; // Name of intrinsic type | ||
} | ||
|
||
// Predicate types (TypeFlags.Predicate) | ||
export interface PredicateType extends Type { | ||
predicate: ThisTypePredicate | IdentifierTypePredicate; | ||
} | ||
|
||
// String literal types (TypeFlags.StringLiteral) | ||
export interface StringLiteralType extends Type { | ||
text: string; // Text of string literal | ||
|
@@ -2239,7 +2264,6 @@ namespace ts { | |
declaration: SignatureDeclaration; // Originating declaration | ||
typeParameters: TypeParameter[]; // Type parameters (undefined if non-generic) | ||
parameters: Symbol[]; // Parameters | ||
typePredicate?: TypePredicate; // Type predicate | ||
/* @internal */ | ||
resolvedReturnType: Type; // Resolved return type | ||
/* @internal */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider just destructuring
parameterName
andtype
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍