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

Lazily compute signature type predicates #17600

Merged
22 commits merged into from
Dec 5, 2017
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
212 changes: 153 additions & 59 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/compiler/symbolWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace ts {
export function createGetSymbolWalker(
getRestTypeOfSignature: (sig: Signature) => Type,
getTypePredicateOfSignature: (sig: Signature) => TypePredicate | undefined,
getReturnTypeOfSignature: (sig: Signature) => Type,
getBaseTypes: (type: Type) => Type[],
resolveStructuredTypeMembers: (type: ObjectType) => ResolvedType,
Expand Down Expand Up @@ -117,8 +118,9 @@ namespace ts {
}

function visitSignature(signature: Signature): void {
if (signature.typePredicate) {
visitType(signature.typePredicate.type);
const typePredicate = getTypePredicateOfSignature(signature);
if (typePredicate) {
visitType(typePredicate.type);
}
forEach(signature.typeParameters, visitType);

Expand Down
22 changes: 18 additions & 4 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2734,7 +2734,17 @@ namespace ts {
/* @internal */ createPromiseType(type: Type): Type;

/* @internal */ createAnonymousType(symbol: Symbol, members: SymbolTable, callSignatures: Signature[], constructSignatures: Signature[], stringIndexInfo: IndexInfo, numberIndexInfo: IndexInfo): Type;
/* @internal */ createSignature(declaration: SignatureDeclaration, typeParameters: TypeParameter[], thisParameter: Symbol | undefined, parameters: Symbol[], resolvedReturnType: Type, typePredicate: TypePredicate, minArgumentCount: number, hasRestParameter: boolean, hasLiteralTypes: boolean): Signature;
/* @internal */ createSignature(
declaration: SignatureDeclaration,
typeParameters: TypeParameter[],
thisParameter: Symbol | undefined,
parameters: Symbol[],
resolvedReturnType: Type,
typePredicate: TypePredicate | undefined,
minArgumentCount: number,
hasRestParameter: boolean,
hasLiteralTypes: boolean,
): Signature;
/* @internal */ createSymbol(flags: SymbolFlags, name: __String): TransientSymbol;
/* @internal */ createIndexInfo(type: Type, isReadonly: boolean, declaration?: SignatureDeclaration): IndexInfo;
/* @internal */ isSymbolAccessible(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags, shouldComputeAliasToMarkVisible: boolean): SymbolAccessibilityResult;
Expand Down Expand Up @@ -3545,7 +3555,13 @@ namespace ts {
/* @internal */
thisParameter?: Symbol; // symbol of this-type parameter
/* @internal */
resolvedReturnType: Type; // Resolved return type
// See comment in `instantiateSignature` for why these are set lazily.
resolvedReturnType: Type | undefined; // Lazily set by `getReturnTypeOfSignature`.
/* @internal */
// Lazily set by `getTypePredicateOfSignature`.
// `undefined` indicates a type predicate that has not yet been computed.
// Uses a special `noTypePredicate` sentinel value to indicate that there is no type predicate. This looks like a TypePredicate at runtime to avoid polymorphism.
resolvedTypePredicate: TypePredicate | undefined;
/* @internal */
minArgumentCount: number; // Number of non-optional parameters
/* @internal */
Expand All @@ -3565,8 +3581,6 @@ namespace ts {
/* @internal */
isolatedSignatureType?: ObjectType; // A manufactured type that just contains the signature for purposes of signature comparison
/* @internal */
typePredicate?: TypePredicate;
/* @internal */
instantiations?: Map<Signature>; // Generic signature instantiation cache
}

Expand Down
1 change: 1 addition & 0 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ namespace ts {
parameters: Symbol[];
thisParameter: Symbol;
resolvedReturnType: Type;
resolvedTypePredicate: TypePredicate | undefined;
minTypeArgumentCount: number;
minArgumentCount: number;
hasRestParameter: boolean;
Expand Down
9 changes: 9 additions & 0 deletions tests/baselines/reference/typeInferenceTypePredicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [typeInferenceTypePredicate.ts]
declare function f<T>(predicate: (x: {}) => x is T): T;
// 'res' should be of type 'number'.
const res = f((n): n is number => true);


//// [typeInferenceTypePredicate.js]
// 'res' should be of type 'number'.
var res = f(function (n) { return true; });
17 changes: 17 additions & 0 deletions tests/baselines/reference/typeInferenceTypePredicate.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/typeInferenceTypePredicate.ts ===
declare function f<T>(predicate: (x: {}) => x is T): T;
>f : Symbol(f, Decl(typeInferenceTypePredicate.ts, 0, 0))
>T : Symbol(T, Decl(typeInferenceTypePredicate.ts, 0, 19))
>predicate : Symbol(predicate, Decl(typeInferenceTypePredicate.ts, 0, 22))
>x : Symbol(x, Decl(typeInferenceTypePredicate.ts, 0, 34))
>x : Symbol(x, Decl(typeInferenceTypePredicate.ts, 0, 34))
>T : Symbol(T, Decl(typeInferenceTypePredicate.ts, 0, 19))
>T : Symbol(T, Decl(typeInferenceTypePredicate.ts, 0, 19))

// 'res' should be of type 'number'.
const res = f((n): n is number => true);
>res : Symbol(res, Decl(typeInferenceTypePredicate.ts, 2, 5))
>f : Symbol(f, Decl(typeInferenceTypePredicate.ts, 0, 0))
>n : Symbol(n, Decl(typeInferenceTypePredicate.ts, 2, 15))
>n : Symbol(n, Decl(typeInferenceTypePredicate.ts, 2, 15))

20 changes: 20 additions & 0 deletions tests/baselines/reference/typeInferenceTypePredicate.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/compiler/typeInferenceTypePredicate.ts ===
declare function f<T>(predicate: (x: {}) => x is T): T;
>f : <T>(predicate: (x: {}) => x is T) => T
>T : T
>predicate : (x: {}) => x is T
>x : {}
>x : any
>T : T
>T : T

// 'res' should be of type 'number'.
const res = f((n): n is number => true);
>res : number
>f((n): n is number => true) : number
>f : <T>(predicate: (x: {}) => x is T) => T
>(n): n is number => true : (n: {}) => n is number
>n : {}
>n : any
>true : true

10 changes: 10 additions & 0 deletions tests/baselines/reference/typeInferenceTypePredicate2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [typeInferenceTypePredicate2.ts]
[true, true, false, null]
.filter((thing): thing is boolean => thing !== null)
.map(thing => thing.toString());


//// [typeInferenceTypePredicate2.js]
[true, true, false, null]
.filter(function (thing) { return thing !== null; })
.map(function (thing) { return thing.toString(); });
18 changes: 18 additions & 0 deletions tests/baselines/reference/typeInferenceTypePredicate2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/typeInferenceTypePredicate2.ts ===
[true, true, false, null]
>[true, true, false, null] .filter((thing): thing is boolean => thing !== null) .map : Symbol(Array.map, Decl(lib.d.ts, --, --))
>[true, true, false, null] .filter : Symbol(Array.filter, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

.filter((thing): thing is boolean => thing !== null)
>filter : Symbol(Array.filter, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>thing : Symbol(thing, Decl(typeInferenceTypePredicate2.ts, 1, 13))
>thing : Symbol(thing, Decl(typeInferenceTypePredicate2.ts, 1, 13))
>thing : Symbol(thing, Decl(typeInferenceTypePredicate2.ts, 1, 13))

.map(thing => thing.toString());
>map : Symbol(Array.map, Decl(lib.d.ts, --, --))
>thing : Symbol(thing, Decl(typeInferenceTypePredicate2.ts, 2, 9))
>thing.toString : Symbol(Object.toString, Decl(lib.d.ts, --, --))
>thing : Symbol(thing, Decl(typeInferenceTypePredicate2.ts, 2, 9))
>toString : Symbol(Object.toString, Decl(lib.d.ts, --, --))

30 changes: 30 additions & 0 deletions tests/baselines/reference/typeInferenceTypePredicate2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/compiler/typeInferenceTypePredicate2.ts ===
[true, true, false, null]
>[true, true, false, null] .filter((thing): thing is boolean => thing !== null) .map(thing => thing.toString()) : string[]
>[true, true, false, null] .filter((thing): thing is boolean => thing !== null) .map : <U>(callbackfn: (value: boolean, index: number, array: boolean[]) => U, thisArg?: any) => U[]
>[true, true, false, null] .filter((thing): thing is boolean => thing !== null) : boolean[]
>[true, true, false, null] .filter : { <S extends boolean>(callbackfn: (value: boolean, index: number, array: boolean[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: boolean, index: number, array: boolean[]) => any, thisArg?: any): boolean[]; }
>[true, true, false, null] : boolean[]
>true : true
>true : true
>false : false
>null : null

.filter((thing): thing is boolean => thing !== null)
>filter : { <S extends boolean>(callbackfn: (value: boolean, index: number, array: boolean[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: boolean, index: number, array: boolean[]) => any, thisArg?: any): boolean[]; }
>(thing): thing is boolean => thing !== null : (thing: boolean) => thing is boolean
>thing : boolean
>thing : any
>thing !== null : boolean
>thing : boolean
>null : null

.map(thing => thing.toString());
>map : <U>(callbackfn: (value: boolean, index: number, array: boolean[]) => U, thisArg?: any) => U[]
>thing => thing.toString() : (thing: boolean) => string
>thing : boolean
>thing.toString() : string
>thing.toString : () => string
>thing : boolean
>toString : () => string

23 changes: 23 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [typePredicatesInUnion.ts]
interface A {
pred(x: {}): x is boolean;
}
interface B {
pred(x: {}): x is string;
}

type Or = A | B;

function f(o: Or, x: {}) {
if (o.pred(x)) {
x;
}
}


//// [typePredicatesInUnion.js]
function f(o, x) {
if (o.pred(x)) {
x;
}
}
40 changes: 40 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== tests/cases/compiler/typePredicatesInUnion.ts ===
interface A {
>A : Symbol(A, Decl(typePredicatesInUnion.ts, 0, 0))

pred(x: {}): x is boolean;
>pred : Symbol(A.pred, Decl(typePredicatesInUnion.ts, 0, 13))
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 1, 9))
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 1, 9))
}
interface B {
>B : Symbol(B, Decl(typePredicatesInUnion.ts, 2, 1))

pred(x: {}): x is string;
>pred : Symbol(B.pred, Decl(typePredicatesInUnion.ts, 3, 13))
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 4, 9))
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 4, 9))
}

type Or = A | B;
>Or : Symbol(Or, Decl(typePredicatesInUnion.ts, 5, 1))
>A : Symbol(A, Decl(typePredicatesInUnion.ts, 0, 0))
>B : Symbol(B, Decl(typePredicatesInUnion.ts, 2, 1))

function f(o: Or, x: {}) {
>f : Symbol(f, Decl(typePredicatesInUnion.ts, 7, 16))
>o : Symbol(o, Decl(typePredicatesInUnion.ts, 9, 11))
>Or : Symbol(Or, Decl(typePredicatesInUnion.ts, 5, 1))
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 9, 17))

if (o.pred(x)) {
>o.pred : Symbol(pred, Decl(typePredicatesInUnion.ts, 0, 13), Decl(typePredicatesInUnion.ts, 3, 13))
>o : Symbol(o, Decl(typePredicatesInUnion.ts, 9, 11))
>pred : Symbol(pred, Decl(typePredicatesInUnion.ts, 0, 13), Decl(typePredicatesInUnion.ts, 3, 13))
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 9, 17))

x;
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 9, 17))
}
}

41 changes: 41 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
=== tests/cases/compiler/typePredicatesInUnion.ts ===
interface A {
>A : A

pred(x: {}): x is boolean;
>pred : (x: {}) => x is boolean
>x : {}
>x : any
}
interface B {
>B : B

pred(x: {}): x is string;
>pred : (x: {}) => x is string
>x : {}
>x : any
}

type Or = A | B;
>Or : Or
>A : A
>B : B

function f(o: Or, x: {}) {
>f : (o: Or, x: {}) => void
>o : Or
>Or : Or
>x : {}

if (o.pred(x)) {
>o.pred(x) : boolean
>o.pred : ((x: {}) => x is boolean) | ((x: {}) => x is string)
>o : Or
>pred : ((x: {}) => x is boolean) | ((x: {}) => x is string)
>x : {}

x;
>x : string | boolean
}
}

11 changes: 11 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [typePredicatesInUnion2.ts]
declare function isString(x: any): x is string;
declare function isNumber(x: any): x is number;
declare function f(p: typeof isString | typeof isNumber): void;
f(isString);
f(isNumber);


//// [typePredicatesInUnion2.js]
f(isString);
f(isNumber);
25 changes: 25 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/compiler/typePredicatesInUnion2.ts ===
declare function isString(x: any): x is string;
>isString : Symbol(isString, Decl(typePredicatesInUnion2.ts, 0, 0))
>x : Symbol(x, Decl(typePredicatesInUnion2.ts, 0, 26))
>x : Symbol(x, Decl(typePredicatesInUnion2.ts, 0, 26))

declare function isNumber(x: any): x is number;
>isNumber : Symbol(isNumber, Decl(typePredicatesInUnion2.ts, 0, 47))
>x : Symbol(x, Decl(typePredicatesInUnion2.ts, 1, 26))
>x : Symbol(x, Decl(typePredicatesInUnion2.ts, 1, 26))

declare function f(p: typeof isString | typeof isNumber): void;
>f : Symbol(f, Decl(typePredicatesInUnion2.ts, 1, 47))
>p : Symbol(p, Decl(typePredicatesInUnion2.ts, 2, 19))
>isString : Symbol(isString, Decl(typePredicatesInUnion2.ts, 0, 0))
>isNumber : Symbol(isNumber, Decl(typePredicatesInUnion2.ts, 0, 47))

f(isString);
>f : Symbol(f, Decl(typePredicatesInUnion2.ts, 1, 47))
>isString : Symbol(isString, Decl(typePredicatesInUnion2.ts, 0, 0))

f(isNumber);
>f : Symbol(f, Decl(typePredicatesInUnion2.ts, 1, 47))
>isNumber : Symbol(isNumber, Decl(typePredicatesInUnion2.ts, 0, 47))

27 changes: 27 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/typePredicatesInUnion2.ts ===
declare function isString(x: any): x is string;
>isString : (x: any) => x is string
>x : any
>x : any

declare function isNumber(x: any): x is number;
>isNumber : (x: any) => x is number
>x : any
>x : any

declare function f(p: typeof isString | typeof isNumber): void;
>f : (p: ((x: any) => x is string) | ((x: any) => x is number)) => void
>p : ((x: any) => x is string) | ((x: any) => x is number)
>isString : (x: any) => x is string
>isNumber : (x: any) => x is number

f(isString);
>f(isString) : void
>f : (p: ((x: any) => x is string) | ((x: any) => x is number)) => void
>isString : (x: any) => x is string

f(isNumber);
>f(isNumber) : void
>f : (p: ((x: any) => x is string) | ((x: any) => x is number)) => void
>isNumber : (x: any) => x is number

25 changes: 25 additions & 0 deletions tests/baselines/reference/typePredicatesInUnion_noMatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [typePredicatesInUnion_noMatch.ts]
interface A {
pred(x: {}, y: {}): x is boolean;
}
interface B {
pred(x: {}, y: {}): y is string;
}

type Or = A | B;

function f(o: Or, x: {}, y: {}) {
if (o.pred(x, y)) {
x;
y;
}
}


//// [typePredicatesInUnion_noMatch.js]
function f(o, x, y) {
if (o.pred(x, y)) {
x;
y;
}
}
Loading