Skip to content

Commit 18e1ac0

Browse files
committed
Fixes #17080
Changes are in src/compiler.checker.ts only The second arguments to the function "removeOptionalityFromDeclaredType" has been changed from "getRootDeclaration(declaration)" to "declaration".
1 parent 92bde08 commit 18e1ac0

7 files changed

+539
-5
lines changed

pull_request_template.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
Thank you for submitting a pull request!
33
44
Here's a checklist you might find useful.
5-
[ ] There is an associated issue that is labelled
5+
[X] There is an associated issue that is labelled
66
'Bug' or 'help wanted' or is in the Community milestone
7-
[ ] Code is up-to-date with the `master` branch
8-
[ ] You've successfully run `jake runtests` locally
7+
[X] Code is up-to-date with the `master` branch
8+
[X] You've successfully run `jake runtests` locally
99
[ ] You've signed the CLA
10-
[ ] There are new or updated unit tests validating the change
10+
[X] There are new or updated unit tests validating the change
1111
1212
Refer to CONTRIBUTING.MD for more details.
1313
https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md
1414
-->
1515

1616
Fixes #
17+
#17080

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13046,7 +13046,7 @@ namespace ts {
1304613046
node.parent.kind === SyntaxKind.NonNullExpression ||
1304713047
declaration.kind === SyntaxKind.VariableDeclaration && (<VariableDeclaration>declaration).exclamationToken ||
1304813048
declaration.flags & NodeFlags.Ambient;
13049-
const initialType = assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, getRootDeclaration(declaration) as VariableLikeDeclaration) : type) :
13049+
const initialType = assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, declaration as VariableLikeDeclaration) : type) :
1305013050
type === autoType || type === autoArrayType ? undefinedType :
1305113051
getOptionalType(type);
1305213052
const flowType = getFlowTypeOfReference(node, type, initialType, flowContainer, !assumeInitialized);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(6,8): error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
2+
Type 'undefined' is not assignable to type 'number'.
3+
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(16,7): error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
4+
Type 'undefined' is not assignable to type 'number'.
5+
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(21,7): error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
6+
Type 'undefined' is not assignable to type 'number'.
7+
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(31,8): error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
8+
Type 'undefined' is not assignable to type 'number'.
9+
10+
11+
==== tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts (4 errors) ====
12+
// https://github.com/Microsoft/TypeScript/issues/17080
13+
function f(a:number,b:number) {
14+
}
15+
16+
function func1( {a, b}: {a: number, b?: number} = {a: 1, b: 2} ) {
17+
f(a, b)
18+
~
19+
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
20+
!!! error TS2345: Type 'undefined' is not assignable to type 'number'.
21+
// error
22+
}
23+
24+
function func2( {a, b = 3}: {a: number, b?:number} = {a: 1,b: 2} ) {
25+
f(a, b)
26+
// no error
27+
}
28+
29+
function func3( {a, b}: {a: number, b?: number} = {a: 1} ) {
30+
f(a,b)
31+
~
32+
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
33+
!!! error TS2345: Type 'undefined' is not assignable to type 'number'.
34+
// error
35+
}
36+
37+
function func4( {a: {b, c}, d}: {a: {b: number,c?: number},d: number} = {a: {b: 1,c: 2},d: 3} ) {
38+
f(b,c)
39+
~
40+
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
41+
!!! error TS2345: Type 'undefined' is not assignable to type 'number'.
42+
// error
43+
}
44+
45+
function func5({a: {b, c = 4}, d}: {a: {b: number,c?: number},d: number} = {a: {b: 1,c: 2},d: 3} ) {
46+
f(b, c)
47+
// no error
48+
}
49+
50+
function func6( {a: {b, c} = {b: 4, c: 5}, d}: {a: {b: number, c?: number}, d: number} = {a: {b: 1,c: 2}, d: 3} ) {
51+
f(b, c)
52+
~
53+
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
54+
!!! error TS2345: Type 'undefined' is not assignable to type 'number'.
55+
// error
56+
}
57+
58+
function func7( {a: {b, c = 6} = {b: 4, c: 5}, d}: {a: {b: number, c?: number}, d: number} = {a: {b: 1, c: 2}, d: 3} ) {
59+
f(b, c)
60+
// no error
61+
}
62+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//// [optionalParameterInDestructuringWithInitializer.ts]
2+
// https://github.com/Microsoft/TypeScript/issues/17080
3+
function f(a:number,b:number) {
4+
}
5+
6+
function func1( {a, b}: {a: number, b?: number} = {a: 1, b: 2} ) {
7+
f(a, b)
8+
// error
9+
}
10+
11+
function func2( {a, b = 3}: {a: number, b?:number} = {a: 1,b: 2} ) {
12+
f(a, b)
13+
// no error
14+
}
15+
16+
function func3( {a, b}: {a: number, b?: number} = {a: 1} ) {
17+
f(a,b)
18+
// error
19+
}
20+
21+
function func4( {a: {b, c}, d}: {a: {b: number,c?: number},d: number} = {a: {b: 1,c: 2},d: 3} ) {
22+
f(b,c)
23+
// error
24+
}
25+
26+
function func5({a: {b, c = 4}, d}: {a: {b: number,c?: number},d: number} = {a: {b: 1,c: 2},d: 3} ) {
27+
f(b, c)
28+
// no error
29+
}
30+
31+
function func6( {a: {b, c} = {b: 4, c: 5}, d}: {a: {b: number, c?: number}, d: number} = {a: {b: 1,c: 2}, d: 3} ) {
32+
f(b, c)
33+
// error
34+
}
35+
36+
function func7( {a: {b, c = 6} = {b: 4, c: 5}, d}: {a: {b: number, c?: number}, d: number} = {a: {b: 1, c: 2}, d: 3} ) {
37+
f(b, c)
38+
// no error
39+
}
40+
41+
42+
//// [optionalParameterInDestructuringWithInitializer.js]
43+
// https://github.com/Microsoft/TypeScript/issues/17080
44+
function f(a, b) {
45+
}
46+
function func1(_a) {
47+
var _b = _a === void 0 ? { a: 1, b: 2 } : _a, a = _b.a, b = _b.b;
48+
f(a, b);
49+
// error
50+
}
51+
function func2(_a) {
52+
var _b = _a === void 0 ? { a: 1, b: 2 } : _a, a = _b.a, _c = _b.b, b = _c === void 0 ? 3 : _c;
53+
f(a, b);
54+
// no error
55+
}
56+
function func3(_a) {
57+
var _b = _a === void 0 ? { a: 1 } : _a, a = _b.a, b = _b.b;
58+
f(a, b);
59+
// error
60+
}
61+
function func4(_a) {
62+
var _b = _a === void 0 ? { a: { b: 1, c: 2 }, d: 3 } : _a, _c = _b.a, b = _c.b, c = _c.c, d = _b.d;
63+
f(b, c);
64+
// error
65+
}
66+
function func5(_a) {
67+
var _b = _a === void 0 ? { a: { b: 1, c: 2 }, d: 3 } : _a, _c = _b.a, b = _c.b, _d = _c.c, c = _d === void 0 ? 4 : _d, d = _b.d;
68+
f(b, c);
69+
// no error
70+
}
71+
function func6(_a) {
72+
var _b = _a === void 0 ? { a: { b: 1, c: 2 }, d: 3 } : _a, _c = _b.a, _d = _c === void 0 ? { b: 4, c: 5 } : _c, b = _d.b, c = _d.c, d = _b.d;
73+
f(b, c);
74+
// error
75+
}
76+
function func7(_a) {
77+
var _b = _a === void 0 ? { a: { b: 1, c: 2 }, d: 3 } : _a, _c = _b.a, _d = _c === void 0 ? { b: 4, c: 5 } : _c, b = _d.b, _e = _d.c, c = _e === void 0 ? 6 : _e, d = _b.d;
78+
f(b, c);
79+
// no error
80+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
=== tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts ===
2+
// https://github.com/Microsoft/TypeScript/issues/17080
3+
function f(a:number,b:number) {
4+
>f : Symbol(f, Decl(optionalParameterInDestructuringWithInitializer.ts, 0, 0))
5+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 1, 11))
6+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 1, 20))
7+
}
8+
9+
function func1( {a, b}: {a: number, b?: number} = {a: 1, b: 2} ) {
10+
>func1 : Symbol(func1, Decl(optionalParameterInDestructuringWithInitializer.ts, 2, 1))
11+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 4, 17))
12+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 4, 19))
13+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 4, 25))
14+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 4, 35))
15+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 4, 51))
16+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 4, 56))
17+
18+
f(a, b)
19+
>f : Symbol(f, Decl(optionalParameterInDestructuringWithInitializer.ts, 0, 0))
20+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 4, 17))
21+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 4, 19))
22+
23+
// error
24+
}
25+
26+
function func2( {a, b = 3}: {a: number, b?:number} = {a: 1,b: 2} ) {
27+
>func2 : Symbol(func2, Decl(optionalParameterInDestructuringWithInitializer.ts, 7, 1))
28+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 9, 17))
29+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 9, 19))
30+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 9, 29))
31+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 9, 39))
32+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 9, 54))
33+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 9, 59))
34+
35+
f(a, b)
36+
>f : Symbol(f, Decl(optionalParameterInDestructuringWithInitializer.ts, 0, 0))
37+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 9, 17))
38+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 9, 19))
39+
40+
// no error
41+
}
42+
43+
function func3( {a, b}: {a: number, b?: number} = {a: 1} ) {
44+
>func3 : Symbol(func3, Decl(optionalParameterInDestructuringWithInitializer.ts, 12, 1))
45+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 14, 17))
46+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 14, 19))
47+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 14, 25))
48+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 14, 35))
49+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 14, 51))
50+
51+
f(a,b)
52+
>f : Symbol(f, Decl(optionalParameterInDestructuringWithInitializer.ts, 0, 0))
53+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 14, 17))
54+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 14, 19))
55+
56+
// error
57+
}
58+
59+
function func4( {a: {b, c}, d}: {a: {b: number,c?: number},d: number} = {a: {b: 1,c: 2},d: 3} ) {
60+
>func4 : Symbol(func4, Decl(optionalParameterInDestructuringWithInitializer.ts, 17, 1))
61+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 19, 33))
62+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 19, 21))
63+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 19, 23))
64+
>d : Symbol(d, Decl(optionalParameterInDestructuringWithInitializer.ts, 19, 27))
65+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 19, 33))
66+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 19, 37))
67+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 19, 47))
68+
>d : Symbol(d, Decl(optionalParameterInDestructuringWithInitializer.ts, 19, 59))
69+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 19, 73))
70+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 19, 77))
71+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 19, 82))
72+
>d : Symbol(d, Decl(optionalParameterInDestructuringWithInitializer.ts, 19, 88))
73+
74+
f(b,c)
75+
>f : Symbol(f, Decl(optionalParameterInDestructuringWithInitializer.ts, 0, 0))
76+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 19, 21))
77+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 19, 23))
78+
79+
// error
80+
}
81+
82+
function func5({a: {b, c = 4}, d}: {a: {b: number,c?: number},d: number} = {a: {b: 1,c: 2},d: 3} ) {
83+
>func5 : Symbol(func5, Decl(optionalParameterInDestructuringWithInitializer.ts, 22, 1))
84+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 24, 36))
85+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 24, 20))
86+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 24, 22))
87+
>d : Symbol(d, Decl(optionalParameterInDestructuringWithInitializer.ts, 24, 30))
88+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 24, 36))
89+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 24, 40))
90+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 24, 50))
91+
>d : Symbol(d, Decl(optionalParameterInDestructuringWithInitializer.ts, 24, 62))
92+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 24, 76))
93+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 24, 80))
94+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 24, 85))
95+
>d : Symbol(d, Decl(optionalParameterInDestructuringWithInitializer.ts, 24, 91))
96+
97+
f(b, c)
98+
>f : Symbol(f, Decl(optionalParameterInDestructuringWithInitializer.ts, 0, 0))
99+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 24, 20))
100+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 24, 22))
101+
102+
// no error
103+
}
104+
105+
function func6( {a: {b, c} = {b: 4, c: 5}, d}: {a: {b: number, c?: number}, d: number} = {a: {b: 1,c: 2}, d: 3} ) {
106+
>func6 : Symbol(func6, Decl(optionalParameterInDestructuringWithInitializer.ts, 27, 1))
107+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 48))
108+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 21))
109+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 23))
110+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 30))
111+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 35))
112+
>d : Symbol(d, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 42))
113+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 48))
114+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 52))
115+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 62))
116+
>d : Symbol(d, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 75))
117+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 90))
118+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 94))
119+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 99))
120+
>d : Symbol(d, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 105))
121+
122+
f(b, c)
123+
>f : Symbol(f, Decl(optionalParameterInDestructuringWithInitializer.ts, 0, 0))
124+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 21))
125+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 29, 23))
126+
127+
// error
128+
}
129+
130+
function func7( {a: {b, c = 6} = {b: 4, c: 5}, d}: {a: {b: number, c?: number}, d: number} = {a: {b: 1, c: 2}, d: 3} ) {
131+
>func7 : Symbol(func7, Decl(optionalParameterInDestructuringWithInitializer.ts, 32, 1))
132+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 52))
133+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 21))
134+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 23))
135+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 34))
136+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 39))
137+
>d : Symbol(d, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 46))
138+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 52))
139+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 56))
140+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 66))
141+
>d : Symbol(d, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 79))
142+
>a : Symbol(a, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 94))
143+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 98))
144+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 103))
145+
>d : Symbol(d, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 110))
146+
147+
f(b, c)
148+
>f : Symbol(f, Decl(optionalParameterInDestructuringWithInitializer.ts, 0, 0))
149+
>b : Symbol(b, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 21))
150+
>c : Symbol(c, Decl(optionalParameterInDestructuringWithInitializer.ts, 34, 23))
151+
152+
// no error
153+
}
154+

0 commit comments

Comments
 (0)