Skip to content

Commit a3387cc

Browse files
authored
Merge pull request microsoft#21328 from Lazarus535/master
Fixes microsoft#17080
2 parents 3735bb6 + 7b449a5 commit a3387cc

6 files changed

+701
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13048,7 +13048,7 @@ namespace ts {
1304813048
node.parent.kind === SyntaxKind.NonNullExpression ||
1304913049
declaration.kind === SyntaxKind.VariableDeclaration && (<VariableDeclaration>declaration).exclamationToken ||
1305013050
declaration.flags & NodeFlags.Ambient;
13051-
const initialType = assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, getRootDeclaration(declaration) as VariableLikeDeclaration) : type) :
13051+
const initialType = assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, declaration as VariableLikeDeclaration) : type) :
1305213052
type === autoType || type === autoArrayType ? undefinedType :
1305313053
getOptionalType(type);
1305413054
const flowType = getFlowTypeOfReference(node, type, initialType, flowContainer, !assumeInitialized);
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(45,10): error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
10+
Type 'undefined' is not assignable to type 'number'.
11+
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(53,11): error TS2345: Argument of type 'number | null' is not assignable to parameter of type 'number | undefined'.
12+
Type 'null' is not assignable to type 'number | undefined'.
13+
14+
15+
==== tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts (6 errors) ====
16+
// https://github.com/Microsoft/TypeScript/issues/17080
17+
18+
declare function f(a:number,b:number): void;
19+
20+
function func1( {a, b}: {a: number, b?: number} = {a: 1, b: 2} ) {
21+
f(a, b)
22+
~
23+
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
24+
!!! error TS2345: Type 'undefined' is not assignable to type 'number'.
25+
// error
26+
}
27+
28+
function func2( {a, b = 3}: {a: number, b?:number} = {a: 1,b: 2} ) {
29+
f(a, b)
30+
// no error
31+
}
32+
33+
function func3( {a, b}: {a: number, b?: number} = {a: 1} ) {
34+
f(a,b)
35+
~
36+
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
37+
!!! error TS2345: Type 'undefined' is not assignable to type 'number'.
38+
// error
39+
}
40+
41+
function func4( {a: {b, c}, d}: {a: {b: number,c?: number},d: number} = {a: {b: 1,c: 2},d: 3} ) {
42+
f(b,c)
43+
~
44+
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
45+
!!! error TS2345: Type 'undefined' is not assignable to type 'number'.
46+
// error
47+
}
48+
49+
function func5({a: {b, c = 4}, d}: {a: {b: number,c?: number},d: number} = {a: {b: 1,c: 2},d: 3} ) {
50+
f(b, c)
51+
// no error
52+
}
53+
54+
function func6( {a: {b, c} = {b: 4, c: 5}, d}: {a: {b: number, c?: number}, d: number} = {a: {b: 1,c: 2}, d: 3} ) {
55+
f(b, c)
56+
~
57+
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
58+
!!! error TS2345: Type 'undefined' is not assignable to type 'number'.
59+
// error
60+
}
61+
62+
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} ) {
63+
f(b, c)
64+
// no error
65+
}
66+
67+
interface Foo {
68+
readonly bar?: number;
69+
}
70+
71+
function performFoo({ bar }: Foo = {}) {
72+
useBar(bar);
73+
~~~
74+
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
75+
!!! error TS2345: Type 'undefined' is not assignable to type 'number'.
76+
}
77+
78+
declare function useBar(bar: number): void;
79+
80+
performFoo();
81+
82+
function performFoo2({ bar = null }: Foo = {}) {
83+
useBar2(bar);
84+
~~~
85+
!!! error TS2345: Argument of type 'number | null' is not assignable to parameter of type 'number | undefined'.
86+
!!! error TS2345: Type 'null' is not assignable to type 'number | undefined'.
87+
}
88+
89+
declare function useBar2(bar: number | undefined): void;
90+
91+
performFoo2();
92+
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//// [optionalParameterInDestructuringWithInitializer.ts]
2+
// https://github.com/Microsoft/TypeScript/issues/17080
3+
4+
declare function f(a:number,b:number): void;
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+
interface Foo {
42+
readonly bar?: number;
43+
}
44+
45+
function performFoo({ bar }: Foo = {}) {
46+
useBar(bar);
47+
}
48+
49+
declare function useBar(bar: number): void;
50+
51+
performFoo();
52+
53+
function performFoo2({ bar = null }: Foo = {}) {
54+
useBar2(bar);
55+
}
56+
57+
declare function useBar2(bar: number | undefined): void;
58+
59+
performFoo2();
60+
61+
62+
//// [optionalParameterInDestructuringWithInitializer.js]
63+
// https://github.com/Microsoft/TypeScript/issues/17080
64+
function func1(_a) {
65+
var _b = _a === void 0 ? { a: 1, b: 2 } : _a, a = _b.a, b = _b.b;
66+
f(a, b);
67+
// error
68+
}
69+
function func2(_a) {
70+
var _b = _a === void 0 ? { a: 1, b: 2 } : _a, a = _b.a, _c = _b.b, b = _c === void 0 ? 3 : _c;
71+
f(a, b);
72+
// no error
73+
}
74+
function func3(_a) {
75+
var _b = _a === void 0 ? { a: 1 } : _a, a = _b.a, b = _b.b;
76+
f(a, b);
77+
// error
78+
}
79+
function func4(_a) {
80+
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;
81+
f(b, c);
82+
// error
83+
}
84+
function func5(_a) {
85+
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;
86+
f(b, c);
87+
// no error
88+
}
89+
function func6(_a) {
90+
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;
91+
f(b, c);
92+
// error
93+
}
94+
function func7(_a) {
95+
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;
96+
f(b, c);
97+
// no error
98+
}
99+
function performFoo(_a) {
100+
var bar = (_a === void 0 ? {} : _a).bar;
101+
useBar(bar);
102+
}
103+
performFoo();
104+
function performFoo2(_a) {
105+
var _b = (_a === void 0 ? {} : _a).bar, bar = _b === void 0 ? null : _b;
106+
useBar2(bar);
107+
}
108+
performFoo2();

0 commit comments

Comments
 (0)