Skip to content

Commit b454644

Browse files
committed
Add regression tests
1 parent 2b33e5c commit b454644

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
indexedAccessConstraints.ts(2,9): error TS2322: Type 'T[keyof T]' is not assignable to type 'number'.
2+
Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'number'.
3+
Type 'T[string]' is not assignable to type 'number'.
4+
5+
6+
==== indexedAccessConstraints.ts (1 errors) ====
7+
function foo<T extends object>(a: T[keyof T]) {
8+
let b: number = a; // Error
9+
~
10+
!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'number'.
11+
!!! error TS2322: Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'number'.
12+
!!! error TS2322: Type 'T[string]' is not assignable to type 'number'.
13+
}
14+
15+
// Repro from #54522
16+
17+
export function methodFnLength<T extends {}, K extends keyof T>(obj: T, methodKey: K): number {
18+
const fn = obj[methodKey];
19+
if (typeof fn !== 'function') {
20+
return 0;
21+
}
22+
return fn.length;
23+
}
24+
25+
// Repro from #54837
26+
27+
function getField<T extends object>(x: T | null, k: keyof T) {
28+
const result = x ? x[k] : null;
29+
return result; // T[keyof T] | null
30+
}
31+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//// [tests/cases/compiler/indexedAccessConstraints.ts] ////
2+
3+
=== indexedAccessConstraints.ts ===
4+
function foo<T extends object>(a: T[keyof T]) {
5+
>foo : Symbol(foo, Decl(indexedAccessConstraints.ts, 0, 0))
6+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 0, 13))
7+
>a : Symbol(a, Decl(indexedAccessConstraints.ts, 0, 31))
8+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 0, 13))
9+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 0, 13))
10+
11+
let b: number = a; // Error
12+
>b : Symbol(b, Decl(indexedAccessConstraints.ts, 1, 7))
13+
>a : Symbol(a, Decl(indexedAccessConstraints.ts, 0, 31))
14+
}
15+
16+
// Repro from #54522
17+
18+
export function methodFnLength<T extends {}, K extends keyof T>(obj: T, methodKey: K): number {
19+
>methodFnLength : Symbol(methodFnLength, Decl(indexedAccessConstraints.ts, 2, 1))
20+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 6, 31))
21+
>K : Symbol(K, Decl(indexedAccessConstraints.ts, 6, 44))
22+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 6, 31))
23+
>obj : Symbol(obj, Decl(indexedAccessConstraints.ts, 6, 64))
24+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 6, 31))
25+
>methodKey : Symbol(methodKey, Decl(indexedAccessConstraints.ts, 6, 71))
26+
>K : Symbol(K, Decl(indexedAccessConstraints.ts, 6, 44))
27+
28+
const fn = obj[methodKey];
29+
>fn : Symbol(fn, Decl(indexedAccessConstraints.ts, 7, 9))
30+
>obj : Symbol(obj, Decl(indexedAccessConstraints.ts, 6, 64))
31+
>methodKey : Symbol(methodKey, Decl(indexedAccessConstraints.ts, 6, 71))
32+
33+
if (typeof fn !== 'function') {
34+
>fn : Symbol(fn, Decl(indexedAccessConstraints.ts, 7, 9))
35+
36+
return 0;
37+
}
38+
return fn.length;
39+
>fn.length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --))
40+
>fn : Symbol(fn, Decl(indexedAccessConstraints.ts, 7, 9))
41+
>length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --))
42+
}
43+
44+
// Repro from #54837
45+
46+
function getField<T extends object>(x: T | null, k: keyof T) {
47+
>getField : Symbol(getField, Decl(indexedAccessConstraints.ts, 12, 1))
48+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 16, 18))
49+
>x : Symbol(x, Decl(indexedAccessConstraints.ts, 16, 36))
50+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 16, 18))
51+
>k : Symbol(k, Decl(indexedAccessConstraints.ts, 16, 48))
52+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 16, 18))
53+
54+
const result = x ? x[k] : null;
55+
>result : Symbol(result, Decl(indexedAccessConstraints.ts, 17, 9))
56+
>x : Symbol(x, Decl(indexedAccessConstraints.ts, 16, 36))
57+
>x : Symbol(x, Decl(indexedAccessConstraints.ts, 16, 36))
58+
>k : Symbol(k, Decl(indexedAccessConstraints.ts, 16, 48))
59+
60+
return result; // T[keyof T] | null
61+
>result : Symbol(result, Decl(indexedAccessConstraints.ts, 17, 9))
62+
}
63+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//// [tests/cases/compiler/indexedAccessConstraints.ts] ////
2+
3+
=== indexedAccessConstraints.ts ===
4+
function foo<T extends object>(a: T[keyof T]) {
5+
>foo : <T extends object>(a: T[keyof T]) => void
6+
>a : T[keyof T]
7+
8+
let b: number = a; // Error
9+
>b : number
10+
>a : T[keyof T]
11+
}
12+
13+
// Repro from #54522
14+
15+
export function methodFnLength<T extends {}, K extends keyof T>(obj: T, methodKey: K): number {
16+
>methodFnLength : <T extends {}, K extends keyof T>(obj: T, methodKey: K) => number
17+
>obj : T
18+
>methodKey : K
19+
20+
const fn = obj[methodKey];
21+
>fn : T[K]
22+
>obj[methodKey] : T[K]
23+
>obj : T
24+
>methodKey : K
25+
26+
if (typeof fn !== 'function') {
27+
>typeof fn !== 'function' : boolean
28+
>typeof fn : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
29+
>fn : T[K]
30+
>'function' : "function"
31+
32+
return 0;
33+
>0 : 0
34+
}
35+
return fn.length;
36+
>fn.length : number
37+
>fn : T[K] & Function
38+
>length : number
39+
}
40+
41+
// Repro from #54837
42+
43+
function getField<T extends object>(x: T | null, k: keyof T) {
44+
>getField : <T extends object>(x: T | null, k: keyof T) => T[keyof T] | null
45+
>x : T | null
46+
>k : keyof T
47+
48+
const result = x ? x[k] : null;
49+
>result : T[keyof T] | null
50+
>x ? x[k] : null : T[keyof T] | null
51+
>x : T | null
52+
>x[k] : T[keyof T]
53+
>x : T
54+
>k : keyof T
55+
56+
return result; // T[keyof T] | null
57+
>result : T[keyof T] | null
58+
}
59+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
function foo<T extends object>(a: T[keyof T]) {
5+
let b: number = a; // Error
6+
}
7+
8+
// Repro from #54522
9+
10+
export function methodFnLength<T extends {}, K extends keyof T>(obj: T, methodKey: K): number {
11+
const fn = obj[methodKey];
12+
if (typeof fn !== 'function') {
13+
return 0;
14+
}
15+
return fn.length;
16+
}
17+
18+
// Repro from #54837
19+
20+
function getField<T extends object>(x: T | null, k: keyof T) {
21+
const result = x ? x[k] : null;
22+
return result; // T[keyof T] | null
23+
}

0 commit comments

Comments
 (0)