From 3d91736f6c40f5316aee09d733fb94d802c1a913 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 30 Mar 2018 16:21:23 -0700 Subject: [PATCH] Accept new baselines --- .../reference/conditionalTypes2.errors.txt | 91 ++++- .../baselines/reference/conditionalTypes2.js | 128 +++++++ .../reference/conditionalTypes2.symbols | 339 ++++++++++++++---- .../reference/conditionalTypes2.types | 228 ++++++++++++ 4 files changed, 720 insertions(+), 66 deletions(-) diff --git a/tests/baselines/reference/conditionalTypes2.errors.txt b/tests/baselines/reference/conditionalTypes2.errors.txt index a4152210268..7e12ca8523b 100644 --- a/tests/baselines/reference/conditionalTypes2.errors.txt +++ b/tests/baselines/reference/conditionalTypes2.errors.txt @@ -10,9 +10,21 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(25,5): error TS23 Types of property 'foo' are incompatible. Type 'A extends string ? keyof A : A' is not assignable to type 'B extends string ? keyof B : B'. Type 'A' is not assignable to type 'B'. +tests/cases/conformance/types/conditional/conditionalTypes2.ts(73,12): error TS2345: Argument of type 'Extract, Bar>' is not assignable to parameter of type '{ foo: string; bat: string; }'. + Type 'Bar & Extract' is not assignable to type '{ foo: string; bat: string; }'. + Property 'bat' is missing in type 'Bar & Foo'. + Type 'Extract' is not assignable to type '{ foo: string; bat: string; }'. + Type 'Bar & Foo & T' is not assignable to type '{ foo: string; bat: string; }'. + Property 'bat' is missing in type 'Bar & Foo'. +tests/cases/conformance/types/conditional/conditionalTypes2.ts(74,12): error TS2345: Argument of type 'Extract' is not assignable to parameter of type '{ foo: string; bat: string; }'. + Type 'Foo & Bar & T' is not assignable to type '{ foo: string; bat: string; }'. + Property 'bat' is missing in type 'Foo & Bar'. +tests/cases/conformance/types/conditional/conditionalTypes2.ts(75,12): error TS2345: Argument of type 'Extract2' is not assignable to parameter of type '{ foo: string; bat: string; }'. + Type 'T extends Bar ? T : never' is not assignable to type '{ foo: string; bat: string; }'. + Type 'Bar & Foo & T' is not assignable to type '{ foo: string; bat: string; }'. -==== tests/cases/conformance/types/conditional/conditionalTypes2.ts (4 errors) ==== +==== tests/cases/conformance/types/conditional/conditionalTypes2.ts (7 errors) ==== interface Covariant { foo: T extends string ? T : number; } @@ -56,6 +68,71 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(25,5): error TS23 !!! error TS2322: Type 'A' is not assignable to type 'B'. } + // Extract is a T that is known to be a Function + function isFunction(value: T): value is Extract { + return typeof value === "function"; + } + + function getFunction(item: T) { + if (isFunction(item)) { + return item; + } + throw new Error(); + } + + function f10(x: T) { + if (isFunction(x)) { + const f: Function = x; + const t: T = x; + } + } + + function f11(x: string | (() => string) | undefined) { + if (isFunction(x)) { + x(); + } + } + + function f12(x: string | (() => string) | undefined) { + const f = getFunction(x); // () => string + f(); + } + + type Foo = { foo: string }; + type Bar = { bar: string }; + + declare function fooBar(x: { foo: string, bar: string }): void; + declare function fooBat(x: { foo: string, bat: string }): void; + + type Extract2 = T extends U ? T extends V ? T : never : never; + + function f20(x: Extract, Bar>, y: Extract, z: Extract2) { + fooBar(x); + fooBar(y); + fooBar(z); + } + + function f21(x: Extract, Bar>, y: Extract, z: Extract2) { + fooBat(x); // Error + ~ +!!! error TS2345: Argument of type 'Extract, Bar>' is not assignable to parameter of type '{ foo: string; bat: string; }'. +!!! error TS2345: Type 'Bar & Extract' is not assignable to type '{ foo: string; bat: string; }'. +!!! error TS2345: Property 'bat' is missing in type 'Bar & Foo'. +!!! error TS2345: Type 'Extract' is not assignable to type '{ foo: string; bat: string; }'. +!!! error TS2345: Type 'Bar & Foo & T' is not assignable to type '{ foo: string; bat: string; }'. +!!! error TS2345: Property 'bat' is missing in type 'Bar & Foo'. + fooBat(y); // Error + ~ +!!! error TS2345: Argument of type 'Extract' is not assignable to parameter of type '{ foo: string; bat: string; }'. +!!! error TS2345: Type 'Foo & Bar & T' is not assignable to type '{ foo: string; bat: string; }'. +!!! error TS2345: Property 'bat' is missing in type 'Foo & Bar'. + fooBat(z); // Error + ~ +!!! error TS2345: Argument of type 'Extract2' is not assignable to parameter of type '{ foo: string; bat: string; }'. +!!! error TS2345: Type 'T extends Bar ? T : never' is not assignable to type '{ foo: string; bat: string; }'. +!!! error TS2345: Type 'Bar & Foo & T' is not assignable to type '{ foo: string; bat: string; }'. + } + // Repros from #22860 class Opt { @@ -87,4 +164,16 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(25,5): error TS23 bat: B1>; boom: T extends any ? true : true } + + // Repro from #22899 + + declare function toString1(value: object | Function): string ; + declare function toString2(value: Function): string ; + + function foo(value: T) { + if (isFunction(value)) { + toString1(value); + toString2(value); + } + } \ No newline at end of file diff --git a/tests/baselines/reference/conditionalTypes2.js b/tests/baselines/reference/conditionalTypes2.js index 5193bae3345..ee2c1fe27fb 100644 --- a/tests/baselines/reference/conditionalTypes2.js +++ b/tests/baselines/reference/conditionalTypes2.js @@ -26,6 +26,56 @@ function f3(a: Invariant, b: Invariant) { b = a; // Error } +// Extract is a T that is known to be a Function +function isFunction(value: T): value is Extract { + return typeof value === "function"; +} + +function getFunction(item: T) { + if (isFunction(item)) { + return item; + } + throw new Error(); +} + +function f10(x: T) { + if (isFunction(x)) { + const f: Function = x; + const t: T = x; + } +} + +function f11(x: string | (() => string) | undefined) { + if (isFunction(x)) { + x(); + } +} + +function f12(x: string | (() => string) | undefined) { + const f = getFunction(x); // () => string + f(); +} + +type Foo = { foo: string }; +type Bar = { bar: string }; + +declare function fooBar(x: { foo: string, bar: string }): void; +declare function fooBat(x: { foo: string, bat: string }): void; + +type Extract2 = T extends U ? T extends V ? T : never : never; + +function f20(x: Extract, Bar>, y: Extract, z: Extract2) { + fooBar(x); + fooBar(y); + fooBar(z); +} + +function f21(x: Extract, Bar>, y: Extract, z: Extract2) { + fooBat(x); // Error + fooBat(y); // Error + fooBat(z); // Error +} + // Repros from #22860 class Opt { @@ -57,6 +107,18 @@ interface B1 extends A1 { bat: B1>; boom: T extends any ? true : true } + +// Repro from #22899 + +declare function toString1(value: object | Function): string ; +declare function toString2(value: Function): string ; + +function foo(value: T) { + if (isFunction(value)) { + toString1(value); + toString2(value); + } +} //// [conditionalTypes2.js] @@ -73,6 +135,41 @@ function f3(a, b) { a = b; // Error b = a; // Error } +// Extract is a T that is known to be a Function +function isFunction(value) { + return typeof value === "function"; +} +function getFunction(item) { + if (isFunction(item)) { + return item; + } + throw new Error(); +} +function f10(x) { + if (isFunction(x)) { + var f = x; + var t = x; + } +} +function f11(x) { + if (isFunction(x)) { + x(); + } +} +function f12(x) { + var f = getFunction(x); // () => string + f(); +} +function f20(x, y, z) { + fooBar(x); + fooBar(y); + fooBar(z); +} +function f21(x, y, z) { + fooBat(x); // Error + fooBat(y); // Error + fooBat(z); // Error +} // Repros from #22860 var Opt = /** @class */ (function () { function Opt() { @@ -93,6 +190,12 @@ var Vector = /** @class */ (function () { }; return Vector; }()); +function foo(value) { + if (isFunction(value)) { + toString1(value); + toString2(value); + } +} //// [conditionalTypes2.d.ts] @@ -108,6 +211,28 @@ interface Invariant { declare function f1(a: Covariant, b: Covariant): void; declare function f2(a: Contravariant, b: Contravariant): void; declare function f3(a: Invariant, b: Invariant): void; +declare function isFunction(value: T): value is Extract; +declare function getFunction(item: T): Extract; +declare function f10(x: T): void; +declare function f11(x: string | (() => string) | undefined): void; +declare function f12(x: string | (() => string) | undefined): void; +declare type Foo = { + foo: string; +}; +declare type Bar = { + bar: string; +}; +declare function fooBar(x: { + foo: string; + bar: string; +}): void; +declare function fooBat(x: { + foo: string; + bat: string; +}): void; +declare type Extract2 = T extends U ? T extends V ? T : never : never; +declare function f20(x: Extract, Bar>, y: Extract, z: Extract2): void; +declare function f21(x: Extract, Bar>, y: Extract, z: Extract2): void; declare class Opt { toVector(): Vector; } @@ -126,3 +251,6 @@ interface B1 extends A1 { bat: B1>; boom: T extends any ? true : true; } +declare function toString1(value: object | Function): string; +declare function toString2(value: Function): string; +declare function foo(value: T): void; diff --git a/tests/baselines/reference/conditionalTypes2.symbols b/tests/baselines/reference/conditionalTypes2.symbols index 4d9cd27238b..ef58dc6229c 100644 --- a/tests/baselines/reference/conditionalTypes2.symbols +++ b/tests/baselines/reference/conditionalTypes2.symbols @@ -93,16 +93,193 @@ function f3(a: Invariant, b: Invariant) { >a : Symbol(a, Decl(conditionalTypes2.ts, 22, 28)) } +// Extract is a T that is known to be a Function +function isFunction(value: T): value is Extract { +>isFunction : Symbol(isFunction, Decl(conditionalTypes2.ts, 25, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 28, 20)) +>value : Symbol(value, Decl(conditionalTypes2.ts, 28, 23)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 28, 20)) +>value : Symbol(value, Decl(conditionalTypes2.ts, 28, 23)) +>Extract : Symbol(Extract, Decl(lib.d.ts, --, --)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 28, 20)) +>Function : Symbol(Function, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) + + return typeof value === "function"; +>value : Symbol(value, Decl(conditionalTypes2.ts, 28, 23)) +} + +function getFunction(item: T) { +>getFunction : Symbol(getFunction, Decl(conditionalTypes2.ts, 30, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 32, 21)) +>item : Symbol(item, Decl(conditionalTypes2.ts, 32, 24)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 32, 21)) + + if (isFunction(item)) { +>isFunction : Symbol(isFunction, Decl(conditionalTypes2.ts, 25, 1)) +>item : Symbol(item, Decl(conditionalTypes2.ts, 32, 24)) + + return item; +>item : Symbol(item, Decl(conditionalTypes2.ts, 32, 24)) + } + throw new Error(); +>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +} + +function f10(x: T) { +>f10 : Symbol(f10, Decl(conditionalTypes2.ts, 37, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 39, 13)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 39, 16)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 39, 13)) + + if (isFunction(x)) { +>isFunction : Symbol(isFunction, Decl(conditionalTypes2.ts, 25, 1)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 39, 16)) + + const f: Function = x; +>f : Symbol(f, Decl(conditionalTypes2.ts, 41, 13)) +>Function : Symbol(Function, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 39, 16)) + + const t: T = x; +>t : Symbol(t, Decl(conditionalTypes2.ts, 42, 13)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 39, 13)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 39, 16)) + } +} + +function f11(x: string | (() => string) | undefined) { +>f11 : Symbol(f11, Decl(conditionalTypes2.ts, 44, 1)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 46, 13)) + + if (isFunction(x)) { +>isFunction : Symbol(isFunction, Decl(conditionalTypes2.ts, 25, 1)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 46, 13)) + + x(); +>x : Symbol(x, Decl(conditionalTypes2.ts, 46, 13)) + } +} + +function f12(x: string | (() => string) | undefined) { +>f12 : Symbol(f12, Decl(conditionalTypes2.ts, 50, 1)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 52, 13)) + + const f = getFunction(x); // () => string +>f : Symbol(f, Decl(conditionalTypes2.ts, 53, 9)) +>getFunction : Symbol(getFunction, Decl(conditionalTypes2.ts, 30, 1)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 52, 13)) + + f(); +>f : Symbol(f, Decl(conditionalTypes2.ts, 53, 9)) +} + +type Foo = { foo: string }; +>Foo : Symbol(Foo, Decl(conditionalTypes2.ts, 55, 1)) +>foo : Symbol(foo, Decl(conditionalTypes2.ts, 57, 12)) + +type Bar = { bar: string }; +>Bar : Symbol(Bar, Decl(conditionalTypes2.ts, 57, 27)) +>bar : Symbol(bar, Decl(conditionalTypes2.ts, 58, 12)) + +declare function fooBar(x: { foo: string, bar: string }): void; +>fooBar : Symbol(fooBar, Decl(conditionalTypes2.ts, 58, 27)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 60, 24)) +>foo : Symbol(foo, Decl(conditionalTypes2.ts, 60, 28)) +>bar : Symbol(bar, Decl(conditionalTypes2.ts, 60, 41)) + +declare function fooBat(x: { foo: string, bat: string }): void; +>fooBat : Symbol(fooBat, Decl(conditionalTypes2.ts, 60, 63)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 61, 24)) +>foo : Symbol(foo, Decl(conditionalTypes2.ts, 61, 28)) +>bat : Symbol(bat, Decl(conditionalTypes2.ts, 61, 41)) + +type Extract2 = T extends U ? T extends V ? T : never : never; +>Extract2 : Symbol(Extract2, Decl(conditionalTypes2.ts, 61, 63)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 63, 14)) +>U : Symbol(U, Decl(conditionalTypes2.ts, 63, 16)) +>V : Symbol(V, Decl(conditionalTypes2.ts, 63, 19)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 63, 14)) +>U : Symbol(U, Decl(conditionalTypes2.ts, 63, 16)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 63, 14)) +>V : Symbol(V, Decl(conditionalTypes2.ts, 63, 19)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 63, 14)) + +function f20(x: Extract, Bar>, y: Extract, z: Extract2) { +>f20 : Symbol(f20, Decl(conditionalTypes2.ts, 63, 71)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 65, 13)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 65, 16)) +>Extract : Symbol(Extract, Decl(lib.d.ts, --, --)) +>Extract : Symbol(Extract, Decl(lib.d.ts, --, --)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 65, 13)) +>Foo : Symbol(Foo, Decl(conditionalTypes2.ts, 55, 1)) +>Bar : Symbol(Bar, Decl(conditionalTypes2.ts, 57, 27)) +>y : Symbol(y, Decl(conditionalTypes2.ts, 65, 49)) +>Extract : Symbol(Extract, Decl(lib.d.ts, --, --)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 65, 13)) +>Foo : Symbol(Foo, Decl(conditionalTypes2.ts, 55, 1)) +>Bar : Symbol(Bar, Decl(conditionalTypes2.ts, 57, 27)) +>z : Symbol(z, Decl(conditionalTypes2.ts, 65, 75)) +>Extract2 : Symbol(Extract2, Decl(conditionalTypes2.ts, 61, 63)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 65, 13)) +>Foo : Symbol(Foo, Decl(conditionalTypes2.ts, 55, 1)) +>Bar : Symbol(Bar, Decl(conditionalTypes2.ts, 57, 27)) + + fooBar(x); +>fooBar : Symbol(fooBar, Decl(conditionalTypes2.ts, 58, 27)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 65, 16)) + + fooBar(y); +>fooBar : Symbol(fooBar, Decl(conditionalTypes2.ts, 58, 27)) +>y : Symbol(y, Decl(conditionalTypes2.ts, 65, 49)) + + fooBar(z); +>fooBar : Symbol(fooBar, Decl(conditionalTypes2.ts, 58, 27)) +>z : Symbol(z, Decl(conditionalTypes2.ts, 65, 75)) +} + +function f21(x: Extract, Bar>, y: Extract, z: Extract2) { +>f21 : Symbol(f21, Decl(conditionalTypes2.ts, 69, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 71, 13)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 71, 16)) +>Extract : Symbol(Extract, Decl(lib.d.ts, --, --)) +>Extract : Symbol(Extract, Decl(lib.d.ts, --, --)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 71, 13)) +>Foo : Symbol(Foo, Decl(conditionalTypes2.ts, 55, 1)) +>Bar : Symbol(Bar, Decl(conditionalTypes2.ts, 57, 27)) +>y : Symbol(y, Decl(conditionalTypes2.ts, 71, 49)) +>Extract : Symbol(Extract, Decl(lib.d.ts, --, --)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 71, 13)) +>Foo : Symbol(Foo, Decl(conditionalTypes2.ts, 55, 1)) +>Bar : Symbol(Bar, Decl(conditionalTypes2.ts, 57, 27)) +>z : Symbol(z, Decl(conditionalTypes2.ts, 71, 75)) +>Extract2 : Symbol(Extract2, Decl(conditionalTypes2.ts, 61, 63)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 71, 13)) +>Foo : Symbol(Foo, Decl(conditionalTypes2.ts, 55, 1)) +>Bar : Symbol(Bar, Decl(conditionalTypes2.ts, 57, 27)) + + fooBat(x); // Error +>fooBat : Symbol(fooBat, Decl(conditionalTypes2.ts, 60, 63)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 71, 16)) + + fooBat(y); // Error +>fooBat : Symbol(fooBat, Decl(conditionalTypes2.ts, 60, 63)) +>y : Symbol(y, Decl(conditionalTypes2.ts, 71, 49)) + + fooBat(z); // Error +>fooBat : Symbol(fooBat, Decl(conditionalTypes2.ts, 60, 63)) +>z : Symbol(z, Decl(conditionalTypes2.ts, 71, 75)) +} + // Repros from #22860 class Opt { ->Opt : Symbol(Opt, Decl(conditionalTypes2.ts, 25, 1)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 29, 10)) +>Opt : Symbol(Opt, Decl(conditionalTypes2.ts, 75, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 79, 10)) toVector(): Vector { ->toVector : Symbol(Opt.toVector, Decl(conditionalTypes2.ts, 29, 14)) ->Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 37, 1)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 29, 10)) +>toVector : Symbol(Opt.toVector, Decl(conditionalTypes2.ts, 79, 14)) +>Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 87, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 79, 10)) return undefined; >undefined : Symbol(undefined) @@ -110,67 +287,67 @@ class Opt { } interface Seq { ->Seq : Symbol(Seq, Decl(conditionalTypes2.ts, 33, 1)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 35, 14)) +>Seq : Symbol(Seq, Decl(conditionalTypes2.ts, 83, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 85, 14)) tail(): Opt>; ->tail : Symbol(Seq.tail, Decl(conditionalTypes2.ts, 35, 18)) ->Opt : Symbol(Opt, Decl(conditionalTypes2.ts, 25, 1)) ->Seq : Symbol(Seq, Decl(conditionalTypes2.ts, 33, 1)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 35, 14)) +>tail : Symbol(Seq.tail, Decl(conditionalTypes2.ts, 85, 18)) +>Opt : Symbol(Opt, Decl(conditionalTypes2.ts, 75, 1)) +>Seq : Symbol(Seq, Decl(conditionalTypes2.ts, 83, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 85, 14)) } class Vector implements Seq { ->Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 37, 1)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 39, 13)) ->Seq : Symbol(Seq, Decl(conditionalTypes2.ts, 33, 1)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 39, 13)) +>Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 87, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 89, 13)) +>Seq : Symbol(Seq, Decl(conditionalTypes2.ts, 83, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 89, 13)) tail(): Opt> { ->tail : Symbol(Vector.tail, Decl(conditionalTypes2.ts, 39, 35)) ->Opt : Symbol(Opt, Decl(conditionalTypes2.ts, 25, 1)) ->Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 37, 1)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 39, 13)) +>tail : Symbol(Vector.tail, Decl(conditionalTypes2.ts, 89, 35)) +>Opt : Symbol(Opt, Decl(conditionalTypes2.ts, 75, 1)) +>Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 87, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 89, 13)) return undefined; >undefined : Symbol(undefined) } partition2(predicate:(v:T)=>v is U): [Vector,Vector>]; ->partition2 : Symbol(Vector.partition2, Decl(conditionalTypes2.ts, 42, 5), Decl(conditionalTypes2.ts, 43, 88), Decl(conditionalTypes2.ts, 44, 64)) ->U : Symbol(U, Decl(conditionalTypes2.ts, 43, 15)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 39, 13)) ->predicate : Symbol(predicate, Decl(conditionalTypes2.ts, 43, 28)) ->v : Symbol(v, Decl(conditionalTypes2.ts, 43, 39)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 39, 13)) ->v : Symbol(v, Decl(conditionalTypes2.ts, 43, 39)) ->U : Symbol(U, Decl(conditionalTypes2.ts, 43, 15)) ->Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 37, 1)) ->U : Symbol(U, Decl(conditionalTypes2.ts, 43, 15)) ->Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 37, 1)) +>partition2 : Symbol(Vector.partition2, Decl(conditionalTypes2.ts, 92, 5), Decl(conditionalTypes2.ts, 93, 88), Decl(conditionalTypes2.ts, 94, 64)) +>U : Symbol(U, Decl(conditionalTypes2.ts, 93, 15)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 89, 13)) +>predicate : Symbol(predicate, Decl(conditionalTypes2.ts, 93, 28)) +>v : Symbol(v, Decl(conditionalTypes2.ts, 93, 39)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 89, 13)) +>v : Symbol(v, Decl(conditionalTypes2.ts, 93, 39)) +>U : Symbol(U, Decl(conditionalTypes2.ts, 93, 15)) +>Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 87, 1)) +>U : Symbol(U, Decl(conditionalTypes2.ts, 93, 15)) +>Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 87, 1)) >Exclude : Symbol(Exclude, Decl(lib.d.ts, --, --)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 39, 13)) ->U : Symbol(U, Decl(conditionalTypes2.ts, 43, 15)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 89, 13)) +>U : Symbol(U, Decl(conditionalTypes2.ts, 93, 15)) partition2(predicate:(x:T)=>boolean): [Vector,Vector]; ->partition2 : Symbol(Vector.partition2, Decl(conditionalTypes2.ts, 42, 5), Decl(conditionalTypes2.ts, 43, 88), Decl(conditionalTypes2.ts, 44, 64)) ->predicate : Symbol(predicate, Decl(conditionalTypes2.ts, 44, 15)) ->x : Symbol(x, Decl(conditionalTypes2.ts, 44, 26)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 39, 13)) ->Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 37, 1)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 39, 13)) ->Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 37, 1)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 39, 13)) +>partition2 : Symbol(Vector.partition2, Decl(conditionalTypes2.ts, 92, 5), Decl(conditionalTypes2.ts, 93, 88), Decl(conditionalTypes2.ts, 94, 64)) +>predicate : Symbol(predicate, Decl(conditionalTypes2.ts, 94, 15)) +>x : Symbol(x, Decl(conditionalTypes2.ts, 94, 26)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 89, 13)) +>Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 87, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 89, 13)) +>Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 87, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 89, 13)) partition2(predicate:(v:T)=>boolean): [Vector,Vector] { ->partition2 : Symbol(Vector.partition2, Decl(conditionalTypes2.ts, 42, 5), Decl(conditionalTypes2.ts, 43, 88), Decl(conditionalTypes2.ts, 44, 64)) ->U : Symbol(U, Decl(conditionalTypes2.ts, 45, 15)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 39, 13)) ->predicate : Symbol(predicate, Decl(conditionalTypes2.ts, 45, 28)) ->v : Symbol(v, Decl(conditionalTypes2.ts, 45, 39)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 39, 13)) ->Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 37, 1)) ->U : Symbol(U, Decl(conditionalTypes2.ts, 45, 15)) ->Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 37, 1)) +>partition2 : Symbol(Vector.partition2, Decl(conditionalTypes2.ts, 92, 5), Decl(conditionalTypes2.ts, 93, 88), Decl(conditionalTypes2.ts, 94, 64)) +>U : Symbol(U, Decl(conditionalTypes2.ts, 95, 15)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 89, 13)) +>predicate : Symbol(predicate, Decl(conditionalTypes2.ts, 95, 28)) +>v : Symbol(v, Decl(conditionalTypes2.ts, 95, 39)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 89, 13)) +>Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 87, 1)) +>U : Symbol(U, Decl(conditionalTypes2.ts, 95, 15)) +>Vector : Symbol(Vector, Decl(conditionalTypes2.ts, 87, 1)) return undefined; >undefined : Symbol(undefined) @@ -178,30 +355,62 @@ class Vector implements Seq { } interface A1 { ->A1 : Symbol(A1, Decl(conditionalTypes2.ts, 48, 1)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 50, 13)) +>A1 : Symbol(A1, Decl(conditionalTypes2.ts, 98, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 100, 13)) bat: B1>; ->bat : Symbol(A1.bat, Decl(conditionalTypes2.ts, 50, 17)) ->B1 : Symbol(B1, Decl(conditionalTypes2.ts, 52, 1)) ->A1 : Symbol(A1, Decl(conditionalTypes2.ts, 48, 1)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 50, 13)) +>bat : Symbol(A1.bat, Decl(conditionalTypes2.ts, 100, 17)) +>B1 : Symbol(B1, Decl(conditionalTypes2.ts, 102, 1)) +>A1 : Symbol(A1, Decl(conditionalTypes2.ts, 98, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 100, 13)) } interface B1 extends A1 { ->B1 : Symbol(B1, Decl(conditionalTypes2.ts, 52, 1)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 54, 13)) ->A1 : Symbol(A1, Decl(conditionalTypes2.ts, 48, 1)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 54, 13)) +>B1 : Symbol(B1, Decl(conditionalTypes2.ts, 102, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 104, 13)) +>A1 : Symbol(A1, Decl(conditionalTypes2.ts, 98, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 104, 13)) bat: B1>; ->bat : Symbol(B1.bat, Decl(conditionalTypes2.ts, 54, 31)) ->B1 : Symbol(B1, Decl(conditionalTypes2.ts, 52, 1)) ->B1 : Symbol(B1, Decl(conditionalTypes2.ts, 52, 1)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 54, 13)) +>bat : Symbol(B1.bat, Decl(conditionalTypes2.ts, 104, 31)) +>B1 : Symbol(B1, Decl(conditionalTypes2.ts, 102, 1)) +>B1 : Symbol(B1, Decl(conditionalTypes2.ts, 102, 1)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 104, 13)) boom: T extends any ? true : true ->boom : Symbol(B1.boom, Decl(conditionalTypes2.ts, 55, 19)) ->T : Symbol(T, Decl(conditionalTypes2.ts, 54, 13)) +>boom : Symbol(B1.boom, Decl(conditionalTypes2.ts, 105, 19)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 104, 13)) +} + +// Repro from #22899 + +declare function toString1(value: object | Function): string ; +>toString1 : Symbol(toString1, Decl(conditionalTypes2.ts, 107, 1)) +>value : Symbol(value, Decl(conditionalTypes2.ts, 111, 27)) +>Function : Symbol(Function, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) + +declare function toString2(value: Function): string ; +>toString2 : Symbol(toString2, Decl(conditionalTypes2.ts, 111, 62)) +>value : Symbol(value, Decl(conditionalTypes2.ts, 112, 27)) +>Function : Symbol(Function, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) + +function foo(value: T) { +>foo : Symbol(foo, Decl(conditionalTypes2.ts, 112, 53)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 114, 13)) +>value : Symbol(value, Decl(conditionalTypes2.ts, 114, 16)) +>T : Symbol(T, Decl(conditionalTypes2.ts, 114, 13)) + + if (isFunction(value)) { +>isFunction : Symbol(isFunction, Decl(conditionalTypes2.ts, 25, 1)) +>value : Symbol(value, Decl(conditionalTypes2.ts, 114, 16)) + + toString1(value); +>toString1 : Symbol(toString1, Decl(conditionalTypes2.ts, 107, 1)) +>value : Symbol(value, Decl(conditionalTypes2.ts, 114, 16)) + + toString2(value); +>toString2 : Symbol(toString2, Decl(conditionalTypes2.ts, 111, 62)) +>value : Symbol(value, Decl(conditionalTypes2.ts, 114, 16)) + } } diff --git a/tests/baselines/reference/conditionalTypes2.types b/tests/baselines/reference/conditionalTypes2.types index e3dad1fc280..249c05f1ad3 100644 --- a/tests/baselines/reference/conditionalTypes2.types +++ b/tests/baselines/reference/conditionalTypes2.types @@ -99,6 +99,199 @@ function f3(a: Invariant, b: Invariant) { >a : Invariant } +// Extract is a T that is known to be a Function +function isFunction(value: T): value is Extract { +>isFunction : (value: T) => value is Extract +>T : T +>value : T +>T : T +>value : any +>Extract : Extract +>T : T +>Function : Function + + return typeof value === "function"; +>typeof value === "function" : boolean +>typeof value : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function" +>value : T +>"function" : "function" +} + +function getFunction(item: T) { +>getFunction : (item: T) => Extract +>T : T +>item : T +>T : T + + if (isFunction(item)) { +>isFunction(item) : boolean +>isFunction : (value: T) => value is Extract +>item : T + + return item; +>item : Extract + } + throw new Error(); +>new Error() : Error +>Error : ErrorConstructor +} + +function f10(x: T) { +>f10 : (x: T) => void +>T : T +>x : T +>T : T + + if (isFunction(x)) { +>isFunction(x) : boolean +>isFunction : (value: T) => value is Extract +>x : T + + const f: Function = x; +>f : Function +>Function : Function +>x : Extract + + const t: T = x; +>t : T +>T : T +>x : Extract + } +} + +function f11(x: string | (() => string) | undefined) { +>f11 : (x: string | (() => string) | undefined) => void +>x : string | (() => string) | undefined + + if (isFunction(x)) { +>isFunction(x) : boolean +>isFunction : (value: T) => value is Extract +>x : string | (() => string) | undefined + + x(); +>x() : string +>x : () => string + } +} + +function f12(x: string | (() => string) | undefined) { +>f12 : (x: string | (() => string) | undefined) => void +>x : string | (() => string) | undefined + + const f = getFunction(x); // () => string +>f : () => string +>getFunction(x) : () => string +>getFunction : (item: T) => Extract +>x : string | (() => string) | undefined + + f(); +>f() : string +>f : () => string +} + +type Foo = { foo: string }; +>Foo : Foo +>foo : string + +type Bar = { bar: string }; +>Bar : Bar +>bar : string + +declare function fooBar(x: { foo: string, bar: string }): void; +>fooBar : (x: { foo: string; bar: string; }) => void +>x : { foo: string; bar: string; } +>foo : string +>bar : string + +declare function fooBat(x: { foo: string, bat: string }): void; +>fooBat : (x: { foo: string; bat: string; }) => void +>x : { foo: string; bat: string; } +>foo : string +>bat : string + +type Extract2 = T extends U ? T extends V ? T : never : never; +>Extract2 : Extract2 +>T : T +>U : U +>V : V +>T : T +>U : U +>T : T +>V : V +>T : T + +function f20(x: Extract, Bar>, y: Extract, z: Extract2) { +>f20 : (x: Extract, Bar>, y: Extract, z: Extract2) => void +>T : T +>x : Extract, Bar> +>Extract : Extract +>Extract : Extract +>T : T +>Foo : Foo +>Bar : Bar +>y : Extract +>Extract : Extract +>T : T +>Foo : Foo +>Bar : Bar +>z : Extract2 +>Extract2 : Extract2 +>T : T +>Foo : Foo +>Bar : Bar + + fooBar(x); +>fooBar(x) : void +>fooBar : (x: { foo: string; bar: string; }) => void +>x : Extract, Bar> + + fooBar(y); +>fooBar(y) : void +>fooBar : (x: { foo: string; bar: string; }) => void +>y : Extract + + fooBar(z); +>fooBar(z) : void +>fooBar : (x: { foo: string; bar: string; }) => void +>z : Extract2 +} + +function f21(x: Extract, Bar>, y: Extract, z: Extract2) { +>f21 : (x: Extract, Bar>, y: Extract, z: Extract2) => void +>T : T +>x : Extract, Bar> +>Extract : Extract +>Extract : Extract +>T : T +>Foo : Foo +>Bar : Bar +>y : Extract +>Extract : Extract +>T : T +>Foo : Foo +>Bar : Bar +>z : Extract2 +>Extract2 : Extract2 +>T : T +>Foo : Foo +>Bar : Bar + + fooBat(x); // Error +>fooBat(x) : void +>fooBat : (x: { foo: string; bat: string; }) => void +>x : Extract, Bar> + + fooBat(y); // Error +>fooBat(y) : void +>fooBat : (x: { foo: string; bat: string; }) => void +>y : Extract + + fooBat(z); // Error +>fooBat(z) : void +>fooBat : (x: { foo: string; bat: string; }) => void +>z : Extract2 +} + // Repros from #22860 class Opt { @@ -216,3 +409,38 @@ interface B1 extends A1 { >true : true } +// Repro from #22899 + +declare function toString1(value: object | Function): string ; +>toString1 : (value: object | Function) => string +>value : object | Function +>Function : Function + +declare function toString2(value: Function): string ; +>toString2 : (value: Function) => string +>value : Function +>Function : Function + +function foo(value: T) { +>foo : (value: T) => void +>T : T +>value : T +>T : T + + if (isFunction(value)) { +>isFunction(value) : boolean +>isFunction : (value: T) => value is Extract +>value : T + + toString1(value); +>toString1(value) : string +>toString1 : (value: object | Function) => string +>value : Extract + + toString2(value); +>toString2(value) : string +>toString2 : (value: Function) => string +>value : Extract + } +} +