File: argumentExpressionContextualTyping.types

package info (click to toggle)
node-typescript 5.0.4%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 459,140 kB
  • sloc: javascript: 1,972,754; makefile: 6; sh: 1
file content (136 lines) | stat: -rw-r--r-- 4,589 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
=== tests/cases/conformance/expressions/contextualTyping/argumentExpressionContextualTyping.ts ===
// In a typed function call, argument expressions are contextually typed by their corresponding parameter types.
function foo({x: [a, b], y: {c, d, e}}) { }
>foo : ({ x: [a, b], y: { c, d, e } }: { x: [any, any]; y: { c: any; d: any; e: any; }; }) => void
>x : any
>a : any
>b : any
>y : any
>c : any
>d : any
>e : any

function bar({x: [a, b = 10], y: {c, d, e = { f:1 }}}) { }
>bar : ({ x: [a, b], y: { c, d, e } }: { x: [any, number?]; y: { c: any; d: any; e?: { f: number; }; }; }) => void
>x : any
>a : any
>b : number
>10 : 10
>y : any
>c : any
>d : any
>e : { f: number; }
>{ f:1 } : { f: number; }
>f : number
>1 : 1

function baz(x: [string, number, boolean]) { }
>baz : (x: [string, number, boolean]) => void
>x : [string, number, boolean]

var o = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
>o : { x: (string | number)[]; y: { c: boolean; d: string; e: number; }; }
>{ x: ["string", 1], y: { c: true, d: "world", e: 3 } } : { x: (string | number)[]; y: { c: boolean; d: string; e: number; }; }
>x : (string | number)[]
>["string", 1] : (string | number)[]
>"string" : "string"
>1 : 1
>y : { c: boolean; d: string; e: number; }
>{ c: true, d: "world", e: 3 } : { c: boolean; d: string; e: number; }
>c : boolean
>true : true
>d : string
>"world" : "world"
>e : number
>3 : 3

var o1: { x: [string, number], y: { c: boolean, d: string, e: number } } = { x: ["string", 1], y: { c: true, d: "world", e: 3 } };
>o1 : { x: [string, number]; y: {    c: boolean;    d: string;    e: number;}; }
>x : [string, number]
>y : { c: boolean; d: string; e: number; }
>c : boolean
>d : string
>e : number
>{ x: ["string", 1], y: { c: true, d: "world", e: 3 } } : { x: [string, number]; y: { c: true; d: string; e: number; }; }
>x : [string, number]
>["string", 1] : [string, number]
>"string" : "string"
>1 : 1
>y : { c: true; d: string; e: number; }
>{ c: true, d: "world", e: 3 } : { c: true; d: string; e: number; }
>c : true
>true : true
>d : string
>"world" : "world"
>e : number
>3 : 3

foo(o1); // Not error since x has contextual type of tuple namely [string, number]
>foo(o1) : void
>foo : ({ x: [a, b], y: { c, d, e } }: { x: [any, any]; y: { c: any; d: any; e: any; }; }) => void
>o1 : { x: [string, number]; y: { c: boolean; d: string; e: number; }; }

foo({ x: ["string", 1], y: { c: true, d: "world", e: 3 } }); // Not error
>foo({ x: ["string", 1], y: { c: true, d: "world", e: 3 } }) : void
>foo : ({ x: [a, b], y: { c, d, e } }: { x: [any, any]; y: { c: any; d: any; e: any; }; }) => void
>{ x: ["string", 1], y: { c: true, d: "world", e: 3 } } : { x: [string, number]; y: { c: boolean; d: string; e: number; }; }
>x : [string, number]
>["string", 1] : [string, number]
>"string" : "string"
>1 : 1
>y : { c: boolean; d: string; e: number; }
>{ c: true, d: "world", e: 3 } : { c: boolean; d: string; e: number; }
>c : boolean
>true : true
>d : string
>"world" : "world"
>e : number
>3 : 3

var array = ["string", 1, true];
>array : (string | number | boolean)[]
>["string", 1, true] : (string | number | boolean)[]
>"string" : "string"
>1 : 1
>true : true

var tuple: [string, number, boolean] = ["string", 1, true];
>tuple : [string, number, boolean]
>["string", 1, true] : [string, number, true]
>"string" : "string"
>1 : 1
>true : true

baz(tuple);
>baz(tuple) : void
>baz : (x: [string, number, boolean]) => void
>tuple : [string, number, boolean]

baz(["string", 1, true]);
>baz(["string", 1, true]) : void
>baz : (x: [string, number, boolean]) => void
>["string", 1, true] : [string, number, true]
>"string" : "string"
>1 : 1
>true : true

baz(array);                          // Error
>baz(array) : void
>baz : (x: [string, number, boolean]) => void
>array : (string | number | boolean)[]

baz(["string", 1, true, ...array]);  // Error
>baz(["string", 1, true, ...array]) : void
>baz : (x: [string, number, boolean]) => void
>["string", 1, true, ...array] : [string, number, true, ...(string | number | boolean)[]]
>"string" : "string"
>1 : 1
>true : true
>...array : string | number | boolean
>array : (string | number | boolean)[]

foo(o);                              // Error because x has an array type namely (string|number)[]
>foo(o) : void
>foo : ({ x: [a, b], y: { c, d, e } }: { x: [any, any]; y: { c: any; d: any; e: any; }; }) => void
>o : { x: (string | number)[]; y: { c: boolean; d: string; e: number; }; }