Skip to content

Commit 98f6761

Browse files
committed
Add tests
1 parent 0bb1f6a commit 98f6761

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
tests/cases/compiler/deferredLookupTypeResolution2.ts(14,13): error TS2536: Type '({ [K in S]: "true"; } & { [key: string]: "false"; })["1"]' cannot be used to index type '{ true: "true"; }'.
2+
tests/cases/compiler/deferredLookupTypeResolution2.ts(19,21): error TS2536: Type '({ true: "otherwise"; } & { [k: string]: "true"; })[({ [K in S]: "true"; } & { [key: string]: "false"; })["1"]]' cannot be used to index type '{ true: "true"; }'.
3+
4+
5+
==== tests/cases/compiler/deferredLookupTypeResolution2.ts (2 errors) ====
6+
// Repro from #17456
7+
8+
type StringContains<S extends string, L extends string> = ({ [K in S]: 'true' } & { [key: string]: 'false'})[L];
9+
10+
type ObjectHasKey<O, L extends string> = StringContains<keyof O, L>;
11+
12+
type A<T> = ObjectHasKey<T, '0'>;
13+
14+
type B = ObjectHasKey<[string, number], '1'>; // "true"
15+
type C = ObjectHasKey<[string, number], '2'>; // "false"
16+
type D = A<[string]>; // "true"
17+
18+
// Error, "false" not handled
19+
type E<T> = { true: 'true' }[ObjectHasKey<T, '1'>];
20+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21+
!!! error TS2536: Type '({ [K in S]: "true"; } & { [key: string]: "false"; })["1"]' cannot be used to index type '{ true: "true"; }'.
22+
23+
type Juxtapose<T> = ({ true: 'otherwise' } & { [k: string]: 'true' })[ObjectHasKey<T, '1'>];
24+
25+
// Error, "otherwise" is missing
26+
type DeepError<T> = { true: 'true' }[Juxtapose<T>];
27+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28+
!!! error TS2536: Type '({ true: "otherwise"; } & { [k: string]: "true"; })[({ [K in S]: "true"; } & { [key: string]: "false"; })["1"]]' cannot be used to index type '{ true: "true"; }'.
29+
30+
type DeepOK<T> = { true: 'true', otherwise: 'false' }[Juxtapose<T>];
31+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//// [deferredLookupTypeResolution2.ts]
2+
// Repro from #17456
3+
4+
type StringContains<S extends string, L extends string> = ({ [K in S]: 'true' } & { [key: string]: 'false'})[L];
5+
6+
type ObjectHasKey<O, L extends string> = StringContains<keyof O, L>;
7+
8+
type A<T> = ObjectHasKey<T, '0'>;
9+
10+
type B = ObjectHasKey<[string, number], '1'>; // "true"
11+
type C = ObjectHasKey<[string, number], '2'>; // "false"
12+
type D = A<[string]>; // "true"
13+
14+
// Error, "false" not handled
15+
type E<T> = { true: 'true' }[ObjectHasKey<T, '1'>];
16+
17+
type Juxtapose<T> = ({ true: 'otherwise' } & { [k: string]: 'true' })[ObjectHasKey<T, '1'>];
18+
19+
// Error, "otherwise" is missing
20+
type DeepError<T> = { true: 'true' }[Juxtapose<T>];
21+
22+
type DeepOK<T> = { true: 'true', otherwise: 'false' }[Juxtapose<T>];
23+
24+
25+
//// [deferredLookupTypeResolution2.js]
26+
"use strict";
27+
// Repro from #17456
28+
29+
30+
//// [deferredLookupTypeResolution2.d.ts]
31+
declare type StringContains<S extends string, L extends string> = ({
32+
[K in S]: 'true';
33+
} & {
34+
[key: string]: 'false';
35+
})[L];
36+
declare type ObjectHasKey<O, L extends string> = StringContains<keyof O, L>;
37+
declare type A<T> = ObjectHasKey<T, '0'>;
38+
declare type B = ObjectHasKey<[string, number], '1'>;
39+
declare type C = ObjectHasKey<[string, number], '2'>;
40+
declare type D = A<[string]>;
41+
declare type E<T> = {
42+
true: 'true';
43+
}[ObjectHasKey<T, '1'>];
44+
declare type Juxtapose<T> = ({
45+
true: 'otherwise';
46+
} & {
47+
[k: string]: 'true';
48+
})[ObjectHasKey<T, '1'>];
49+
declare type DeepError<T> = {
50+
true: 'true';
51+
}[Juxtapose<T>];
52+
declare type DeepOK<T> = {
53+
true: 'true';
54+
otherwise: 'false';
55+
}[Juxtapose<T>];
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// @strict: true
2+
// @declaration: true
3+
4+
// Repro from #17456
5+
6+
type StringContains<S extends string, L extends string> = ({ [K in S]: 'true' } & { [key: string]: 'false'})[L];
7+
8+
type ObjectHasKey<O, L extends string> = StringContains<keyof O, L>;
9+
10+
type A<T> = ObjectHasKey<T, '0'>;
11+
12+
type B = ObjectHasKey<[string, number], '1'>; // "true"
13+
type C = ObjectHasKey<[string, number], '2'>; // "false"
14+
type D = A<[string]>; // "true"
15+
16+
// Error, "false" not handled
17+
type E<T> = { true: 'true' }[ObjectHasKey<T, '1'>];
18+
19+
type Juxtapose<T> = ({ true: 'otherwise' } & { [k: string]: 'true' })[ObjectHasKey<T, '1'>];
20+
21+
// Error, "otherwise" is missing
22+
type DeepError<T> = { true: 'true' }[Juxtapose<T>];
23+
24+
type DeepOK<T> = { true: 'true', otherwise: 'false' }[Juxtapose<T>];

0 commit comments

Comments
 (0)