Skip to content

Commit e01fa85

Browse files
committed
Accept new baselines
1 parent 739acee commit e01fa85

File tree

4 files changed

+1154
-0
lines changed

4 files changed

+1154
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(4,5): error TS2322: Type '"x"' is not assignable to type 'number'.
2+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(6,5): error TS2322: Type '2' is not assignable to type '0 | 1'.
3+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(7,5): error TS2322: Type '"x"' is not assignable to type '0 | 1'.
4+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(8,5): error TS2322: Type '1' is not assignable to type 'never'.
5+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(9,5): error TS2322: Type '2' is not assignable to type 'never'.
6+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(10,5): error TS2322: Type '"x"' is not assignable to type 'never'.
7+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(14,5): error TS2739: Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y
8+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(15,5): error TS2322: Type 'T' is not assignable to type '{ x: number; y: number; }'.
9+
Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y
10+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(18,5): error TS2322: Type '{ x: number; y: number; }' is not assignable to type 'T'.
11+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(19,5): error TS2322: Type '{ [key: string]: number; }' is not assignable to type 'T'.
12+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(26,7): error TS2339: Property 'x' does not exist on type 'T'.
13+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(27,5): error TS2322: Type '1' is not assignable to type 'T[keyof T]'.
14+
Type '1' is not assignable to type 'T[string] & T[number]'.
15+
Type '1' is not assignable to type 'T[string]'.
16+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(31,5): error TS2322: Type '{ [key: string]: number; }' is not assignable to type '{ [P in K]: number; }'.
17+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(45,3): error TS7017: Element implicitly has an 'any' type because type 'Item' has no index signature.
18+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(46,3): error TS2322: Type '123' is not assignable to type 'string & number'.
19+
Type '123' is not assignable to type 'string'.
20+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(47,3): error TS2322: Type '123' is not assignable to type 'T[keyof T]'.
21+
Type '123' is not assignable to type 'T["a"] & T["b"]'.
22+
Type '123' is not assignable to type 'T["a"]'.
23+
Type '123' is not assignable to type 'string'.
24+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(48,3): error TS2322: Type '123' is not assignable to type 'T[K]'.
25+
Type '123' is not assignable to type 'T["a"] & T["b"]'.
26+
Type '123' is not assignable to type 'T["a"]'.
27+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(60,7): error TS2339: Property 'foo' does not exist on type 'T'.
28+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(61,3): error TS2536: Type 'string' cannot be used to index type 'T'.
29+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(62,3): error TS2322: Type '123' is not assignable to type 'T[keyof T]'.
30+
Type '123' is not assignable to type 'T[string]'.
31+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(63,3): error TS2322: Type '123' is not assignable to type 'T[K]'.
32+
Type '123' is not assignable to type 'T[string]'.
33+
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(103,5): error TS2322: Type '123' is not assignable to type 'Type[K]'.
34+
Type '123' is not assignable to type '123 & "some string"'.
35+
Type '123' is not assignable to type '"some string"'.
36+
37+
38+
==== tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts (22 errors) ====
39+
function f1(obj: { a: number, b: 0 | 1, c: string }, k0: 'a', k1: 'a' | 'b', k2: 'a' | 'b' | 'c') {
40+
obj[k0] = 1;
41+
obj[k0] = 2;
42+
obj[k0] = 'x'; // Error
43+
~~~~~~~
44+
!!! error TS2322: Type '"x"' is not assignable to type 'number'.
45+
obj[k1] = 1;
46+
obj[k1] = 2; // Error
47+
~~~~~~~
48+
!!! error TS2322: Type '2' is not assignable to type '0 | 1'.
49+
obj[k1] = 'x'; // Error
50+
~~~~~~~
51+
!!! error TS2322: Type '"x"' is not assignable to type '0 | 1'.
52+
obj[k2] = 1; // Error
53+
~~~~~~~
54+
!!! error TS2322: Type '1' is not assignable to type 'never'.
55+
obj[k2] = 2; // Error
56+
~~~~~~~
57+
!!! error TS2322: Type '2' is not assignable to type 'never'.
58+
obj[k2] = 'x'; // Error
59+
~~~~~~~
60+
!!! error TS2322: Type '"x"' is not assignable to type 'never'.
61+
}
62+
63+
function f2<T extends { [key: string]: number }>(a: { x: number, y: number }, b: { [key: string]: number }, c: T, k: keyof T) {
64+
a = b; // Error, index signature in source doesn't imply properties are present
65+
~
66+
!!! error TS2739: Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y
67+
a = c; // Error, index signature in source doesn't imply properties are present
68+
~
69+
!!! error TS2322: Type 'T' is not assignable to type '{ x: number; y: number; }'.
70+
!!! error TS2322: Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y
71+
b = a;
72+
b = c;
73+
c = a; // Error, constraint on target doesn't imply any properties or signatures
74+
~
75+
!!! error TS2322: Type '{ x: number; y: number; }' is not assignable to type 'T'.
76+
c = b; // Error, constraint on target doesn't imply any properties or signatures
77+
~
78+
!!! error TS2322: Type '{ [key: string]: number; }' is not assignable to type 'T'.
79+
a.x;
80+
b.x;
81+
c.x;
82+
c[k];
83+
a.x = 1;
84+
b.x = 1;
85+
c.x = 1; // Error, cannot write to index signature through constraint
86+
~
87+
!!! error TS2339: Property 'x' does not exist on type 'T'.
88+
c[k] = 1; // Error, cannot write to index signature through constraint
89+
~~~~
90+
!!! error TS2322: Type '1' is not assignable to type 'T[keyof T]'.
91+
!!! error TS2322: Type '1' is not assignable to type 'T[string] & T[number]'.
92+
!!! error TS2322: Type '1' is not assignable to type 'T[string]'.
93+
}
94+
95+
function f3<K extends string>(a: { [P in K]: number }, b: { [key: string]: number }, k: K) {
96+
a = b; // Error, index signature doesn't imply properties are present
97+
~
98+
!!! error TS2322: Type '{ [key: string]: number; }' is not assignable to type '{ [P in K]: number; }'.
99+
b = a;
100+
a[k];
101+
a[k] = 1;
102+
}
103+
104+
function f4<K extends string>(a: { [key: string]: number }[K], b: number) {
105+
a = b;
106+
b = a;
107+
}
108+
109+
type Item = { a: string, b: number };
110+
111+
function f10<T extends Item, K extends keyof T>(obj: T, k1: string, k2: keyof Item, k3: keyof T, k4: K) {
112+
obj[k1] = 123; // Error
113+
~~~~~~~
114+
!!! error TS7017: Element implicitly has an 'any' type because type 'Item' has no index signature.
115+
obj[k2] = 123; // Error
116+
~~~~~~~
117+
!!! error TS2322: Type '123' is not assignable to type 'string & number'.
118+
!!! error TS2322: Type '123' is not assignable to type 'string'.
119+
obj[k3] = 123; // Error
120+
~~~~~~~
121+
!!! error TS2322: Type '123' is not assignable to type 'T[keyof T]'.
122+
!!! error TS2322: Type '123' is not assignable to type 'T["a"] & T["b"]'.
123+
!!! error TS2322: Type '123' is not assignable to type 'T["a"]'.
124+
!!! error TS2322: Type '123' is not assignable to type 'string'.
125+
obj[k4] = 123; // Error
126+
~~~~~~~
127+
!!! error TS2322: Type '123' is not assignable to type 'T[K]'.
128+
!!! error TS2322: Type '123' is not assignable to type 'T["a"] & T["b"]'.
129+
!!! error TS2322: Type '123' is not assignable to type 'T["a"]'.
130+
}
131+
132+
type Dict = Record<string, number>;
133+
134+
function f11<K extends keyof Dict>(obj: Dict, k1: keyof Dict, k2: K) {
135+
obj.foo = 123;
136+
obj[k1] = 123;
137+
obj[k2] = 123;
138+
}
139+
140+
function f12<T extends Readonly<Dict>, K extends keyof T>(obj: T, k1: keyof Dict, k2: keyof T, k3: K) {
141+
obj.foo = 123; // Error
142+
~~~
143+
!!! error TS2339: Property 'foo' does not exist on type 'T'.
144+
obj[k1] = 123; // Error
145+
~~~~~~~
146+
!!! error TS2536: Type 'string' cannot be used to index type 'T'.
147+
obj[k2] = 123; // Error
148+
~~~~~~~
149+
!!! error TS2322: Type '123' is not assignable to type 'T[keyof T]'.
150+
!!! error TS2322: Type '123' is not assignable to type 'T[string]'.
151+
obj[k3] = 123; // Error
152+
~~~~~~~
153+
!!! error TS2322: Type '123' is not assignable to type 'T[K]'.
154+
!!! error TS2322: Type '123' is not assignable to type 'T[string]'.
155+
}
156+
157+
// Repro from #27895
158+
159+
export interface Entity {
160+
id: number | string;
161+
}
162+
163+
export type IdOf<E extends Entity> = E['id'];
164+
165+
export interface EntityState<E extends Entity> {
166+
ids: IdOf<E>[];
167+
entities: { [key: string]: E, [key: number]: E };
168+
}
169+
170+
171+
export function getAllEntities<E extends Entity>(state: EntityState<E>): E[] {
172+
const { ids, entities } = state;
173+
return ids.map(id => entities[id]);
174+
}
175+
176+
export function getEntity<E extends Entity>(id: IdOf<E>, state: EntityState<E>): E | undefined {
177+
const { ids, entities } = state;
178+
179+
if (!ids.includes(id)) {
180+
return undefined;
181+
}
182+
183+
return entities[id];
184+
}
185+
186+
// Repro from #30603
187+
188+
interface Type {
189+
a: 123;
190+
b: "some string";
191+
}
192+
193+
function get123<K extends keyof Type>(): Type[K] {
194+
return 123; // Error
195+
~~~~~~~~~~~
196+
!!! error TS2322: Type '123' is not assignable to type 'Type[K]'.
197+
!!! error TS2322: Type '123' is not assignable to type '123 & "some string"'.
198+
!!! error TS2322: Type '123' is not assignable to type '"some string"'.
199+
}
200+
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
//// [keyofAndIndexedAccess2.ts]
2+
function f1(obj: { a: number, b: 0 | 1, c: string }, k0: 'a', k1: 'a' | 'b', k2: 'a' | 'b' | 'c') {
3+
obj[k0] = 1;
4+
obj[k0] = 2;
5+
obj[k0] = 'x'; // Error
6+
obj[k1] = 1;
7+
obj[k1] = 2; // Error
8+
obj[k1] = 'x'; // Error
9+
obj[k2] = 1; // Error
10+
obj[k2] = 2; // Error
11+
obj[k2] = 'x'; // Error
12+
}
13+
14+
function f2<T extends { [key: string]: number }>(a: { x: number, y: number }, b: { [key: string]: number }, c: T, k: keyof T) {
15+
a = b; // Error, index signature in source doesn't imply properties are present
16+
a = c; // Error, index signature in source doesn't imply properties are present
17+
b = a;
18+
b = c;
19+
c = a; // Error, constraint on target doesn't imply any properties or signatures
20+
c = b; // Error, constraint on target doesn't imply any properties or signatures
21+
a.x;
22+
b.x;
23+
c.x;
24+
c[k];
25+
a.x = 1;
26+
b.x = 1;
27+
c.x = 1; // Error, cannot write to index signature through constraint
28+
c[k] = 1; // Error, cannot write to index signature through constraint
29+
}
30+
31+
function f3<K extends string>(a: { [P in K]: number }, b: { [key: string]: number }, k: K) {
32+
a = b; // Error, index signature doesn't imply properties are present
33+
b = a;
34+
a[k];
35+
a[k] = 1;
36+
}
37+
38+
function f4<K extends string>(a: { [key: string]: number }[K], b: number) {
39+
a = b;
40+
b = a;
41+
}
42+
43+
type Item = { a: string, b: number };
44+
45+
function f10<T extends Item, K extends keyof T>(obj: T, k1: string, k2: keyof Item, k3: keyof T, k4: K) {
46+
obj[k1] = 123; // Error
47+
obj[k2] = 123; // Error
48+
obj[k3] = 123; // Error
49+
obj[k4] = 123; // Error
50+
}
51+
52+
type Dict = Record<string, number>;
53+
54+
function f11<K extends keyof Dict>(obj: Dict, k1: keyof Dict, k2: K) {
55+
obj.foo = 123;
56+
obj[k1] = 123;
57+
obj[k2] = 123;
58+
}
59+
60+
function f12<T extends Readonly<Dict>, K extends keyof T>(obj: T, k1: keyof Dict, k2: keyof T, k3: K) {
61+
obj.foo = 123; // Error
62+
obj[k1] = 123; // Error
63+
obj[k2] = 123; // Error
64+
obj[k3] = 123; // Error
65+
}
66+
67+
// Repro from #27895
68+
69+
export interface Entity {
70+
id: number | string;
71+
}
72+
73+
export type IdOf<E extends Entity> = E['id'];
74+
75+
export interface EntityState<E extends Entity> {
76+
ids: IdOf<E>[];
77+
entities: { [key: string]: E, [key: number]: E };
78+
}
79+
80+
81+
export function getAllEntities<E extends Entity>(state: EntityState<E>): E[] {
82+
const { ids, entities } = state;
83+
return ids.map(id => entities[id]);
84+
}
85+
86+
export function getEntity<E extends Entity>(id: IdOf<E>, state: EntityState<E>): E | undefined {
87+
const { ids, entities } = state;
88+
89+
if (!ids.includes(id)) {
90+
return undefined;
91+
}
92+
93+
return entities[id];
94+
}
95+
96+
// Repro from #30603
97+
98+
interface Type {
99+
a: 123;
100+
b: "some string";
101+
}
102+
103+
function get123<K extends keyof Type>(): Type[K] {
104+
return 123; // Error
105+
}
106+
107+
108+
//// [keyofAndIndexedAccess2.js]
109+
function f1(obj, k0, k1, k2) {
110+
obj[k0] = 1;
111+
obj[k0] = 2;
112+
obj[k0] = 'x'; // Error
113+
obj[k1] = 1;
114+
obj[k1] = 2; // Error
115+
obj[k1] = 'x'; // Error
116+
obj[k2] = 1; // Error
117+
obj[k2] = 2; // Error
118+
obj[k2] = 'x'; // Error
119+
}
120+
function f2(a, b, c, k) {
121+
a = b; // Error, index signature in source doesn't imply properties are present
122+
a = c; // Error, index signature in source doesn't imply properties are present
123+
b = a;
124+
b = c;
125+
c = a; // Error, constraint on target doesn't imply any properties or signatures
126+
c = b; // Error, constraint on target doesn't imply any properties or signatures
127+
a.x;
128+
b.x;
129+
c.x;
130+
c[k];
131+
a.x = 1;
132+
b.x = 1;
133+
c.x = 1; // Error, cannot write to index signature through constraint
134+
c[k] = 1; // Error, cannot write to index signature through constraint
135+
}
136+
function f3(a, b, k) {
137+
a = b; // Error, index signature doesn't imply properties are present
138+
b = a;
139+
a[k];
140+
a[k] = 1;
141+
}
142+
function f4(a, b) {
143+
a = b;
144+
b = a;
145+
}
146+
function f10(obj, k1, k2, k3, k4) {
147+
obj[k1] = 123; // Error
148+
obj[k2] = 123; // Error
149+
obj[k3] = 123; // Error
150+
obj[k4] = 123; // Error
151+
}
152+
function f11(obj, k1, k2) {
153+
obj.foo = 123;
154+
obj[k1] = 123;
155+
obj[k2] = 123;
156+
}
157+
function f12(obj, k1, k2, k3) {
158+
obj.foo = 123; // Error
159+
obj[k1] = 123; // Error
160+
obj[k2] = 123; // Error
161+
obj[k3] = 123; // Error
162+
}
163+
export function getAllEntities(state) {
164+
const { ids, entities } = state;
165+
return ids.map(id => entities[id]);
166+
}
167+
export function getEntity(id, state) {
168+
const { ids, entities } = state;
169+
if (!ids.includes(id)) {
170+
return undefined;
171+
}
172+
return entities[id];
173+
}
174+
function get123() {
175+
return 123; // Error
176+
}

0 commit comments

Comments
 (0)