Skip to content

Commit 900ef91

Browse files
committed
Add tests
1 parent ac9d334 commit 900ef91

File tree

5 files changed

+1862
-0
lines changed

5 files changed

+1862
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferences.ts(123,5): error TS2322: Type '(inputs: Unwrap<{ num: Wrapper<number>; str: Wrapper<string>; }>) => { bool: any; str: number; }' is not assignable to type '(inputs: Unwrap<{ num: Wrapper<number>; str: Wrapper<string>; }>) => Unwrap<{ bool: Wrapper<boolean>; str: Wrapper<string>; }>'.
2+
Call signature return types '{ bool: any; str: number; }' and 'Unwrap<{ bool: Wrapper<boolean>; str: Wrapper<string>; }>' are incompatible.
3+
The types of 'str' are incompatible between these types.
4+
Type 'number' is not assignable to type 'string'.
5+
tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferences.ts(125,26): error TS2339: Property 'nonexistent' does not exist on type 'Unwrap<{ num: Wrapper<number>; str: Wrapper<string>; }>'.
6+
7+
8+
==== tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferences.ts (2 errors) ====
9+
// Repros from #47599
10+
11+
declare function callIt<T>(obj: {
12+
produce: (n: number) => T,
13+
consume: (x: T) => void
14+
}): void;
15+
16+
callIt({
17+
produce: () => 0,
18+
consume: n => n.toFixed()
19+
});
20+
21+
callIt({
22+
produce: _a => 0,
23+
consume: n => n.toFixed(),
24+
});
25+
26+
callIt({
27+
produce() {
28+
return 0;
29+
},
30+
consume: n => n.toFixed()
31+
});
32+
33+
declare function callItT<T>(obj: [(n: number) => T, (x: T) => void]): void;
34+
35+
callItT([() => 0, n => n.toFixed()]);
36+
callItT([_a => 0, n => n.toFixed()]);
37+
38+
// Repro from #25092
39+
40+
interface MyInterface<T> {
41+
retrieveGeneric: (parameter: string) => T,
42+
operateWithGeneric: (generic: T) => string
43+
}
44+
45+
const inferTypeFn = <T>(generic: MyInterface<T>) => generic;
46+
47+
const myGeneric = inferTypeFn({
48+
retrieveGeneric: parameter => 5,
49+
operateWithGeneric: generic => generic.toFixed()
50+
});
51+
52+
// Repro #38623
53+
54+
function make<M>(o: { mutations: M, action: (m: M) => void }) { }
55+
56+
make({
57+
mutations: {
58+
foo() { }
59+
},
60+
action: (a) => { a.foo() }
61+
});
62+
63+
// Repro from #38845
64+
65+
declare function foo<A>(options: { a: A, b: (a: A) => void }): void;
66+
67+
foo({
68+
a: () => { return 42 },
69+
b(a) {},
70+
});
71+
72+
foo({
73+
a: function () { return 42 },
74+
b(a) {},
75+
});
76+
77+
foo({
78+
a() { return 42 },
79+
b(a) {},
80+
});
81+
82+
// Repro from #38872
83+
84+
type Chain<R1, R2> = {
85+
a(): R1,
86+
b(a: R1): R2;
87+
c(b: R2): void;
88+
};
89+
90+
function test<R1, R2>(foo: Chain<R1, R2>) {}
91+
92+
test({
93+
a: () => 0,
94+
b: (a) => 'a',
95+
c: (b) => {
96+
const x: string = b;
97+
}
98+
});
99+
100+
// Repro from #41712
101+
102+
class Wrapper<T = any> {
103+
public value?: T;
104+
}
105+
106+
type WrappedMap = Record<string, Wrapper>;
107+
type Unwrap<D extends WrappedMap> = {
108+
[K in keyof D]: D[K] extends Wrapper<infer T> ? T : never;
109+
};
110+
111+
type MappingComponent<I extends WrappedMap, O extends WrappedMap> = {
112+
setup(): { inputs: I; outputs: O };
113+
map?: (inputs: Unwrap<I>) => Unwrap<O>;
114+
};
115+
116+
declare function createMappingComponent<I extends WrappedMap, O extends WrappedMap>(def: MappingComponent<I, O>): void;
117+
118+
createMappingComponent({
119+
setup() {
120+
return {
121+
inputs: {
122+
num: new Wrapper<number>(),
123+
str: new Wrapper<string>()
124+
},
125+
outputs: {
126+
bool: new Wrapper<boolean>(),
127+
str: new Wrapper<string>()
128+
}
129+
};
130+
},
131+
map(inputs) {
132+
~~~
133+
!!! error TS2322: Type '(inputs: Unwrap<{ num: Wrapper<number>; str: Wrapper<string>; }>) => { bool: any; str: number; }' is not assignable to type '(inputs: Unwrap<{ num: Wrapper<number>; str: Wrapper<string>; }>) => Unwrap<{ bool: Wrapper<boolean>; str: Wrapper<string>; }>'.
134+
!!! error TS2322: Call signature return types '{ bool: any; str: number; }' and 'Unwrap<{ bool: Wrapper<boolean>; str: Wrapper<string>; }>' are incompatible.
135+
!!! error TS2322: The types of 'str' are incompatible between these types.
136+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
137+
!!! related TS6500 tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferences.ts:105:5: The expected type comes from property 'map' which is declared here on type 'MappingComponent<{ num: Wrapper<number>; str: Wrapper<string>; }, { bool: Wrapper<boolean>; str: Wrapper<string>; }>'
138+
return {
139+
bool: inputs.nonexistent,
140+
~~~~~~~~~~~
141+
!!! error TS2339: Property 'nonexistent' does not exist on type 'Unwrap<{ num: Wrapper<number>; str: Wrapper<string>; }>'.
142+
str: inputs.num, // Causes error
143+
}
144+
}
145+
});
146+
147+
// Repro from #48279
148+
149+
function simplified<T>(props: { generator: () => T, receiver: (t: T) => any }) {}
150+
151+
function whatIWant<T>(props: { generator: (bob: any) => T, receiver: (t: T) => any }) {}
152+
153+
function nonObject<T>(generator: (bob: any) => T, receiver: (t: T) => any) {}
154+
155+
simplified({ generator: () => 123, receiver: (t) => console.log(t + 2) })
156+
whatIWant({ generator: (bob) => bob ? 1 : 2, receiver: (t) => console.log(t + 2) })
157+
nonObject((bob) => bob ? 1 : 2, (t) => console.log(t + 2))
158+
159+
// Repro from #48466
160+
161+
interface Opts<TParams, TDone, TMapped> {
162+
fetch: (params: TParams, foo: number) => TDone,
163+
map: (data: TDone) => TMapped
164+
}
165+
166+
function example<TParams, TDone, TMapped>(options: Opts<TParams, TDone, TMapped>) {
167+
return (params: TParams) => {
168+
const data = options.fetch(params, 123)
169+
return options.map(data)
170+
}
171+
}
172+
173+
interface Params {
174+
one: number
175+
two: string
176+
}
177+
178+
example({
179+
fetch: (params: Params) => 123,
180+
map: (number) => String(number)
181+
});
182+
183+
example({
184+
fetch: (params: Params, foo: number) => 123,
185+
map: (number) => String(number)
186+
});
187+
188+
example({
189+
fetch: (params: Params, foo) => 123,
190+
map: (number) => String(number)
191+
});
192+

0 commit comments

Comments
 (0)