Skip to content

Commit a4009a3

Browse files
authored
Add extra tests for contextual typing of unannotated parameters with default initializers (microsoft#53940)
1 parent 02bb310 commit a4009a3

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
=== tests/cases/conformance/expressions/typeSatisfaction/typeSatisfaction_contextualTyping3.ts ===
2+
// see https://github.com/microsoft/TypeScript/issues/53920#issuecomment-1516616255
3+
4+
const obj = {
5+
>obj : Symbol(obj, Decl(typeSatisfaction_contextualTyping3.ts, 2, 5))
6+
7+
foo: (param = "default") => param,
8+
>foo : Symbol(foo, Decl(typeSatisfaction_contextualTyping3.ts, 2, 13))
9+
>param : Symbol(param, Decl(typeSatisfaction_contextualTyping3.ts, 3, 9))
10+
>param : Symbol(param, Decl(typeSatisfaction_contextualTyping3.ts, 3, 9))
11+
12+
} satisfies {
13+
[key: string]: (...params: any) => any;
14+
>key : Symbol(key, Decl(typeSatisfaction_contextualTyping3.ts, 5, 4))
15+
>params : Symbol(params, Decl(typeSatisfaction_contextualTyping3.ts, 5, 19))
16+
17+
};
18+
19+
const obj2 = {
20+
>obj2 : Symbol(obj2, Decl(typeSatisfaction_contextualTyping3.ts, 8, 5))
21+
22+
foo: (param = "default") => param,
23+
>foo : Symbol(foo, Decl(typeSatisfaction_contextualTyping3.ts, 8, 14))
24+
>param : Symbol(param, Decl(typeSatisfaction_contextualTyping3.ts, 9, 9))
25+
>param : Symbol(param, Decl(typeSatisfaction_contextualTyping3.ts, 9, 9))
26+
27+
} satisfies {
28+
[key: string]: Function;
29+
>key : Symbol(key, Decl(typeSatisfaction_contextualTyping3.ts, 11, 4))
30+
>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
31+
32+
};
33+
34+
type StringOrNumberFunc = (x: string | number) => any;
35+
>StringOrNumberFunc : Symbol(StringOrNumberFunc, Decl(typeSatisfaction_contextualTyping3.ts, 12, 2))
36+
>x : Symbol(x, Decl(typeSatisfaction_contextualTyping3.ts, 14, 27))
37+
38+
const fn = ((x = "ok") => null) satisfies StringOrNumberFunc;
39+
>fn : Symbol(fn, Decl(typeSatisfaction_contextualTyping3.ts, 16, 5))
40+
>x : Symbol(x, Decl(typeSatisfaction_contextualTyping3.ts, 16, 13))
41+
>StringOrNumberFunc : Symbol(StringOrNumberFunc, Decl(typeSatisfaction_contextualTyping3.ts, 12, 2))
42+
43+
fn();
44+
>fn : Symbol(fn, Decl(typeSatisfaction_contextualTyping3.ts, 16, 5))
45+
46+
fn(32);
47+
>fn : Symbol(fn, Decl(typeSatisfaction_contextualTyping3.ts, 16, 5))
48+
49+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
=== tests/cases/conformance/expressions/typeSatisfaction/typeSatisfaction_contextualTyping3.ts ===
2+
// see https://github.com/microsoft/TypeScript/issues/53920#issuecomment-1516616255
3+
4+
const obj = {
5+
>obj : { foo: (param?: any) => any; }
6+
>{ foo: (param = "default") => param,} satisfies { [key: string]: (...params: any) => any;} : { foo: (param?: any) => any; }
7+
>{ foo: (param = "default") => param,} : { foo: (param?: any) => any; }
8+
9+
foo: (param = "default") => param,
10+
>foo : (param?: any) => any
11+
>(param = "default") => param : (param?: any) => any
12+
>param : any
13+
>"default" : "default"
14+
>param : any
15+
16+
} satisfies {
17+
[key: string]: (...params: any) => any;
18+
>key : string
19+
>params : any
20+
21+
};
22+
23+
const obj2 = {
24+
>obj2 : { foo: (param?: string) => string; }
25+
>{ foo: (param = "default") => param,} satisfies { [key: string]: Function;} : { foo: (param?: string) => string; }
26+
>{ foo: (param = "default") => param,} : { foo: (param?: string) => string; }
27+
28+
foo: (param = "default") => param,
29+
>foo : (param?: string) => string
30+
>(param = "default") => param : (param?: string) => string
31+
>param : string
32+
>"default" : "default"
33+
>param : string
34+
35+
} satisfies {
36+
[key: string]: Function;
37+
>key : string
38+
39+
};
40+
41+
type StringOrNumberFunc = (x: string | number) => any;
42+
>StringOrNumberFunc : (x: string | number) => any
43+
>x : string | number
44+
45+
const fn = ((x = "ok") => null) satisfies StringOrNumberFunc;
46+
>fn : (x?: string | number) => null
47+
>((x = "ok") => null) satisfies StringOrNumberFunc : (x?: string | number) => null
48+
>((x = "ok") => null) : (x?: string | number) => null
49+
>(x = "ok") => null : (x?: string | number) => null
50+
>x : string | number
51+
>"ok" : "ok"
52+
53+
fn();
54+
>fn() : null
55+
>fn : (x?: string | number) => null
56+
57+
fn(32);
58+
>fn(32) : null
59+
>fn : (x?: string | number) => null
60+
>32 : 32
61+
62+
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+
// see https://github.com/microsoft/TypeScript/issues/53920#issuecomment-1516616255
5+
6+
const obj = {
7+
foo: (param = "default") => param,
8+
} satisfies {
9+
[key: string]: (...params: any) => any;
10+
};
11+
12+
const obj2 = {
13+
foo: (param = "default") => param,
14+
} satisfies {
15+
[key: string]: Function;
16+
};
17+
18+
type StringOrNumberFunc = (x: string | number) => any;
19+
20+
const fn = ((x = "ok") => null) satisfies StringOrNumberFunc;
21+
fn();
22+
fn(32);
23+

0 commit comments

Comments
 (0)