Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahejlsberg committed Oct 6, 2023
1 parent 5082d17 commit 2f5d9e9
Show file tree
Hide file tree
Showing 4 changed files with 621 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
distributiveConditionalTypeConstraints.ts(4,9): error TS2322: Type 'boolean' is not assignable to type 'true'.
distributiveConditionalTypeConstraints.ts(5,9): error TS2322: Type 'boolean' is not assignable to type 'false'.
distributiveConditionalTypeConstraints.ts(10,9): error TS2322: Type 'IsArray<T>' is not assignable to type 'false'.
Type 'true' is not assignable to type 'false'.
distributiveConditionalTypeConstraints.ts(15,9): error TS2322: Type 'IsArray<T>' is not assignable to type 'false'.
Type 'true' is not assignable to type 'false'.
distributiveConditionalTypeConstraints.ts(19,9): error TS2322: Type 'IsArray<T>' is not assignable to type 'true'.
Type 'false' is not assignable to type 'true'.


==== distributiveConditionalTypeConstraints.ts (5 errors) ====
type IsArray<T> = T extends unknown[] ? true : false;

function f1<T extends object>(x: IsArray<T>) {
let t: true = x; // Error
~
!!! error TS2322: Type 'boolean' is not assignable to type 'true'.
let f: false = x; // Error
~
!!! error TS2322: Type 'boolean' is not assignable to type 'false'.
}

function f2<T extends unknown[]>(x: IsArray<T>) {
let t: true = x;
let f: false = x; // Error
~
!!! error TS2322: Type 'IsArray<T>' is not assignable to type 'false'.
!!! error TS2322: Type 'true' is not assignable to type 'false'.
}

function f3<T extends string[]>(x: IsArray<T>) {
let t: true = x;
let f: false = x; // Error
~
!!! error TS2322: Type 'IsArray<T>' is not assignable to type 'false'.
!!! error TS2322: Type 'true' is not assignable to type 'false'.
}

function f4<T extends Function>(x: IsArray<T>) {
let t: true = x; // Error
~
!!! error TS2322: Type 'IsArray<T>' is not assignable to type 'true'.
!!! error TS2322: Type 'false' is not assignable to type 'true'.
let f: false = x;
}

type ZeroOf<T> =
T extends null ? null :
T extends undefined ? undefined :
T extends string ? "" :
T extends number ? 0 :
T extends boolean ? false :
never;

function f10<T extends {}>(x: ZeroOf<T>) {
let t: "" | 0 | false = x;
}

// Modified repro from #30152

interface A { foo(): void; }
interface B { bar(): void; }
interface C { foo(): void, bar(): void }

function test1<T extends A>(y: T extends B ? number : string) {
if (typeof y == 'string') {
y; // T extends B ? number : string
}
else {
y; // never
}
const newY: string | number = y;
newY; // string
}

function test2<T extends A>(y: T extends B ? string : number) {
if (typeof y == 'string') {
y; // never
}
else {
y; // T extends B ? string : number
}
const newY: string | number = y;
newY; // number
}

function test3<T extends A>(y: T extends C ? number : string) {
if (typeof y == 'string') {
y; // (T extends C ? number : string) & string
}
else {
y; // T extends C ? number : string
}
const newY: string | number = y;
newY; // string | number
}

function test4<T extends A>(y: T extends C ? string : number) {
if (typeof y == 'string') {
y; // (T extends C ? string : number) & string
}
else {
y; // T extends C ? string : number
}
const newY: string | number = y;
newY; // string | number
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
//// [tests/cases/compiler/distributiveConditionalTypeConstraints.ts] ////

=== distributiveConditionalTypeConstraints.ts ===
type IsArray<T> = T extends unknown[] ? true : false;
>IsArray : Symbol(IsArray, Decl(distributiveConditionalTypeConstraints.ts, 0, 0))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 0, 13))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 0, 13))

function f1<T extends object>(x: IsArray<T>) {
>f1 : Symbol(f1, Decl(distributiveConditionalTypeConstraints.ts, 0, 53))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 2, 12))
>x : Symbol(x, Decl(distributiveConditionalTypeConstraints.ts, 2, 30))
>IsArray : Symbol(IsArray, Decl(distributiveConditionalTypeConstraints.ts, 0, 0))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 2, 12))

let t: true = x; // Error
>t : Symbol(t, Decl(distributiveConditionalTypeConstraints.ts, 3, 7))
>x : Symbol(x, Decl(distributiveConditionalTypeConstraints.ts, 2, 30))

let f: false = x; // Error
>f : Symbol(f, Decl(distributiveConditionalTypeConstraints.ts, 4, 7))
>x : Symbol(x, Decl(distributiveConditionalTypeConstraints.ts, 2, 30))
}

function f2<T extends unknown[]>(x: IsArray<T>) {
>f2 : Symbol(f2, Decl(distributiveConditionalTypeConstraints.ts, 5, 1))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 7, 12))
>x : Symbol(x, Decl(distributiveConditionalTypeConstraints.ts, 7, 33))
>IsArray : Symbol(IsArray, Decl(distributiveConditionalTypeConstraints.ts, 0, 0))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 7, 12))

let t: true = x;
>t : Symbol(t, Decl(distributiveConditionalTypeConstraints.ts, 8, 7))
>x : Symbol(x, Decl(distributiveConditionalTypeConstraints.ts, 7, 33))

let f: false = x; // Error
>f : Symbol(f, Decl(distributiveConditionalTypeConstraints.ts, 9, 7))
>x : Symbol(x, Decl(distributiveConditionalTypeConstraints.ts, 7, 33))
}

function f3<T extends string[]>(x: IsArray<T>) {
>f3 : Symbol(f3, Decl(distributiveConditionalTypeConstraints.ts, 10, 1))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 12, 12))
>x : Symbol(x, Decl(distributiveConditionalTypeConstraints.ts, 12, 32))
>IsArray : Symbol(IsArray, Decl(distributiveConditionalTypeConstraints.ts, 0, 0))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 12, 12))

let t: true = x;
>t : Symbol(t, Decl(distributiveConditionalTypeConstraints.ts, 13, 7))
>x : Symbol(x, Decl(distributiveConditionalTypeConstraints.ts, 12, 32))

let f: false = x; // Error
>f : Symbol(f, Decl(distributiveConditionalTypeConstraints.ts, 14, 7))
>x : Symbol(x, Decl(distributiveConditionalTypeConstraints.ts, 12, 32))
}

function f4<T extends Function>(x: IsArray<T>) {
>f4 : Symbol(f4, Decl(distributiveConditionalTypeConstraints.ts, 15, 1))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 17, 12))
>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(distributiveConditionalTypeConstraints.ts, 17, 32))
>IsArray : Symbol(IsArray, Decl(distributiveConditionalTypeConstraints.ts, 0, 0))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 17, 12))

let t: true = x; // Error
>t : Symbol(t, Decl(distributiveConditionalTypeConstraints.ts, 18, 7))
>x : Symbol(x, Decl(distributiveConditionalTypeConstraints.ts, 17, 32))

let f: false = x;
>f : Symbol(f, Decl(distributiveConditionalTypeConstraints.ts, 19, 7))
>x : Symbol(x, Decl(distributiveConditionalTypeConstraints.ts, 17, 32))
}

type ZeroOf<T> =
>ZeroOf : Symbol(ZeroOf, Decl(distributiveConditionalTypeConstraints.ts, 20, 1))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 22, 12))

T extends null ? null :
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 22, 12))

T extends undefined ? undefined :
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 22, 12))

T extends string ? "" :
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 22, 12))

T extends number ? 0 :
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 22, 12))

T extends boolean ? false :
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 22, 12))

never;

function f10<T extends {}>(x: ZeroOf<T>) {
>f10 : Symbol(f10, Decl(distributiveConditionalTypeConstraints.ts, 28, 10))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 30, 13))
>x : Symbol(x, Decl(distributiveConditionalTypeConstraints.ts, 30, 27))
>ZeroOf : Symbol(ZeroOf, Decl(distributiveConditionalTypeConstraints.ts, 20, 1))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 30, 13))

let t: "" | 0 | false = x;
>t : Symbol(t, Decl(distributiveConditionalTypeConstraints.ts, 31, 7))
>x : Symbol(x, Decl(distributiveConditionalTypeConstraints.ts, 30, 27))
}

// Modified repro from #30152

interface A { foo(): void; }
>A : Symbol(A, Decl(distributiveConditionalTypeConstraints.ts, 32, 1))
>foo : Symbol(A.foo, Decl(distributiveConditionalTypeConstraints.ts, 36, 13))

interface B { bar(): void; }
>B : Symbol(B, Decl(distributiveConditionalTypeConstraints.ts, 36, 28))
>bar : Symbol(B.bar, Decl(distributiveConditionalTypeConstraints.ts, 37, 13))

interface C { foo(): void, bar(): void }
>C : Symbol(C, Decl(distributiveConditionalTypeConstraints.ts, 37, 28))
>foo : Symbol(C.foo, Decl(distributiveConditionalTypeConstraints.ts, 38, 13))
>bar : Symbol(C.bar, Decl(distributiveConditionalTypeConstraints.ts, 38, 26))

function test1<T extends A>(y: T extends B ? number : string) {
>test1 : Symbol(test1, Decl(distributiveConditionalTypeConstraints.ts, 38, 40))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 40, 15))
>A : Symbol(A, Decl(distributiveConditionalTypeConstraints.ts, 32, 1))
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 40, 28))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 40, 15))
>B : Symbol(B, Decl(distributiveConditionalTypeConstraints.ts, 36, 28))

if (typeof y == 'string') {
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 40, 28))

y; // T extends B ? number : string
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 40, 28))
}
else {
y; // never
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 40, 28))
}
const newY: string | number = y;
>newY : Symbol(newY, Decl(distributiveConditionalTypeConstraints.ts, 47, 9))
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 40, 28))

newY; // string
>newY : Symbol(newY, Decl(distributiveConditionalTypeConstraints.ts, 47, 9))
}

function test2<T extends A>(y: T extends B ? string : number) {
>test2 : Symbol(test2, Decl(distributiveConditionalTypeConstraints.ts, 49, 1))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 51, 15))
>A : Symbol(A, Decl(distributiveConditionalTypeConstraints.ts, 32, 1))
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 51, 28))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 51, 15))
>B : Symbol(B, Decl(distributiveConditionalTypeConstraints.ts, 36, 28))

if (typeof y == 'string') {
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 51, 28))

y; // never
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 51, 28))
}
else {
y; // T extends B ? string : number
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 51, 28))
}
const newY: string | number = y;
>newY : Symbol(newY, Decl(distributiveConditionalTypeConstraints.ts, 58, 9))
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 51, 28))

newY; // number
>newY : Symbol(newY, Decl(distributiveConditionalTypeConstraints.ts, 58, 9))
}

function test3<T extends A>(y: T extends C ? number : string) {
>test3 : Symbol(test3, Decl(distributiveConditionalTypeConstraints.ts, 60, 1))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 62, 15))
>A : Symbol(A, Decl(distributiveConditionalTypeConstraints.ts, 32, 1))
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 62, 28))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 62, 15))
>C : Symbol(C, Decl(distributiveConditionalTypeConstraints.ts, 37, 28))

if (typeof y == 'string') {
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 62, 28))

y; // (T extends C ? number : string) & string
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 62, 28))
}
else {
y; // T extends C ? number : string
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 62, 28))
}
const newY: string | number = y;
>newY : Symbol(newY, Decl(distributiveConditionalTypeConstraints.ts, 69, 9))
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 62, 28))

newY; // string | number
>newY : Symbol(newY, Decl(distributiveConditionalTypeConstraints.ts, 69, 9))
}

function test4<T extends A>(y: T extends C ? string : number) {
>test4 : Symbol(test4, Decl(distributiveConditionalTypeConstraints.ts, 71, 1))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 73, 15))
>A : Symbol(A, Decl(distributiveConditionalTypeConstraints.ts, 32, 1))
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 73, 28))
>T : Symbol(T, Decl(distributiveConditionalTypeConstraints.ts, 73, 15))
>C : Symbol(C, Decl(distributiveConditionalTypeConstraints.ts, 37, 28))

if (typeof y == 'string') {
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 73, 28))

y; // (T extends C ? string : number) & string
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 73, 28))
}
else {
y; // T extends C ? string : number
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 73, 28))
}
const newY: string | number = y;
>newY : Symbol(newY, Decl(distributiveConditionalTypeConstraints.ts, 80, 9))
>y : Symbol(y, Decl(distributiveConditionalTypeConstraints.ts, 73, 28))

newY; // string | number
>newY : Symbol(newY, Decl(distributiveConditionalTypeConstraints.ts, 80, 9))
}

Loading

0 comments on commit 2f5d9e9

Please sign in to comment.