Skip to content

Commit b70d079

Browse files
committed
Merge pull request microsoft#8845 from Microsoft/Fix8834
Fix microsoft#8834: exclude paramters and variables from flag checks
2 parents 0a623f8 + 04d6e91 commit b70d079

8 files changed

+91
-0
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14528,6 +14528,12 @@ namespace ts {
1452814528
}
1452914529

1453014530
function areDeclarationFlagsIdentical(left: Declaration, right: Declaration) {
14531+
if ((left.kind === SyntaxKind.Parameter && right.kind === SyntaxKind.VariableDeclaration) ||
14532+
(left.kind === SyntaxKind.VariableDeclaration && right.kind === SyntaxKind.Parameter)) {
14533+
// Differences in optionality between parameters and variables are allowed.
14534+
return true;
14535+
}
14536+
1453114537
if (hasQuestionToken(left) !== hasQuestionToken(right)) {
1453214538
return false;
1453314539
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [optionalParamterAndVariableDeclaration.ts]
2+
class C {
3+
constructor(options?: number) {
4+
var options = (options || 0);
5+
}
6+
}
7+
8+
9+
//// [optionalParamterAndVariableDeclaration.js]
10+
var C = (function () {
11+
function C(options) {
12+
var options = (options || 0);
13+
}
14+
return C;
15+
}());
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/optionalParamterAndVariableDeclaration.ts ===
2+
class C {
3+
>C : Symbol(C, Decl(optionalParamterAndVariableDeclaration.ts, 0, 0))
4+
5+
constructor(options?: number) {
6+
>options : Symbol(options, Decl(optionalParamterAndVariableDeclaration.ts, 1, 16), Decl(optionalParamterAndVariableDeclaration.ts, 2, 11))
7+
8+
var options = (options || 0);
9+
>options : Symbol(options, Decl(optionalParamterAndVariableDeclaration.ts, 1, 16), Decl(optionalParamterAndVariableDeclaration.ts, 2, 11))
10+
>options : Symbol(options, Decl(optionalParamterAndVariableDeclaration.ts, 1, 16), Decl(optionalParamterAndVariableDeclaration.ts, 2, 11))
11+
}
12+
}
13+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/optionalParamterAndVariableDeclaration.ts ===
2+
class C {
3+
>C : C
4+
5+
constructor(options?: number) {
6+
>options : number
7+
8+
var options = (options || 0);
9+
>options : number
10+
>(options || 0) : number
11+
>options || 0 : number
12+
>options : number
13+
>0 : number
14+
}
15+
}
16+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/compiler/optionalParamterAndVariableDeclaration2.ts(4,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'options' must be of type 'number | undefined', but here has type 'number'.
2+
3+
4+
==== tests/cases/compiler/optionalParamterAndVariableDeclaration2.ts (1 errors) ====
5+
6+
class C {
7+
constructor(options?: number) {
8+
var options = (options || 0);
9+
~~~~~~~
10+
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'options' must be of type 'number | undefined', but here has type 'number'.
11+
}
12+
}
13+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [optionalParamterAndVariableDeclaration2.ts]
2+
3+
class C {
4+
constructor(options?: number) {
5+
var options = (options || 0);
6+
}
7+
}
8+
9+
10+
//// [optionalParamterAndVariableDeclaration2.js]
11+
var C = (function () {
12+
function C(options) {
13+
var options = (options || 0);
14+
}
15+
return C;
16+
}());
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class C {
2+
constructor(options?: number) {
3+
var options = (options || 0);
4+
}
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @strictNullChecks: true
2+
3+
class C {
4+
constructor(options?: number) {
5+
var options = (options || 0);
6+
}
7+
}

0 commit comments

Comments
 (0)