Skip to content

Commit f9ad30e

Browse files
a-tarasyukZzzen
authored andcommitted
fix(41965): fix error in definite assignment assertion context (microsoft#41989)
1 parent e830df4 commit f9ad30e

File tree

6 files changed

+40
-6
lines changed

6 files changed

+40
-6
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40139,7 +40139,9 @@ namespace ts {
4013940139
if (node.exclamationToken && (node.parent.parent.kind !== SyntaxKind.VariableStatement || !node.type || node.initializer || node.flags & NodeFlags.Ambient)) {
4014040140
const message = node.initializer
4014140141
? Diagnostics.Declarations_with_initializers_cannot_also_have_definite_assignment_assertions
40142-
: Diagnostics.Declarations_with_definite_assignment_assertions_must_also_have_type_annotations;
40142+
: !node.type
40143+
? Diagnostics.Declarations_with_definite_assignment_assertions_must_also_have_type_annotations
40144+
: Diagnostics.A_definite_assignment_assertion_is_not_permitted_in_this_context;
4014340145
return grammarErrorOnNode(node.exclamationToken, message);
4014440146
}
4014540147

tests/baselines/reference/definiteAssignmentAssertions.errors.txt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(35,15): erro
99
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(69,10): error TS1264: Declarations with definite assignment assertions must also have type annotations.
1010
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(70,10): error TS1263: Declarations with initializers cannot also have definite assignment assertions.
1111
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(71,10): error TS1263: Declarations with initializers cannot also have definite assignment assertions.
12-
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(76,15): error TS1264: Declarations with definite assignment assertions must also have type annotations.
13-
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(77,15): error TS1264: Declarations with definite assignment assertions must also have type annotations.
12+
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(76,15): error TS1255: A definite assignment assertion '!' is not permitted in this context.
13+
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(77,15): error TS1255: A definite assignment assertion '!' is not permitted in this context.
14+
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(80,7): error TS1255: A definite assignment assertion '!' is not permitted in this context.
1415

1516

16-
==== tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts (13 errors) ====
17+
==== tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts (14 errors) ====
1718
// Suppress strict property initialization check
1819

1920
class C1 {
@@ -113,8 +114,14 @@ tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(77,15): erro
113114

114115
declare let v1!: number;
115116
~
116-
!!! error TS1264: Declarations with definite assignment assertions must also have type annotations.
117+
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context.
117118
declare var v2!: number;
118119
~
119-
!!! error TS1264: Declarations with definite assignment assertions must also have type annotations.
120+
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context.
121+
122+
declare namespace foo {
123+
var v!: number;
124+
~
125+
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context.
126+
}
120127

tests/baselines/reference/definiteAssignmentAssertions.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ function f4() {
7676

7777
declare let v1!: number;
7878
declare var v2!: number;
79+
80+
declare namespace foo {
81+
var v!: number;
82+
}
7983

8084

8185
//// [definiteAssignmentAssertions.js]
@@ -166,3 +170,6 @@ declare function f3(): void;
166170
declare function f4(): void;
167171
declare let v1: number;
168172
declare var v2: number;
173+
declare namespace foo {
174+
var v: number;
175+
}

tests/baselines/reference/definiteAssignmentAssertions.symbols

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,10 @@ declare let v1!: number;
147147
declare var v2!: number;
148148
>v2 : Symbol(v2, Decl(definiteAssignmentAssertions.ts, 76, 11))
149149

150+
declare namespace foo {
151+
>foo : Symbol(foo, Decl(definiteAssignmentAssertions.ts, 76, 24))
152+
153+
var v!: number;
154+
>v : Symbol(v, Decl(definiteAssignmentAssertions.ts, 79, 4))
155+
}
156+

tests/baselines/reference/definiteAssignmentAssertions.types

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,10 @@ declare let v1!: number;
158158
declare var v2!: number;
159159
>v2 : number
160160

161+
declare namespace foo {
162+
>foo : typeof foo
163+
164+
var v!: number;
165+
>v : number
166+
}
167+

tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,7 @@ function f4() {
7878

7979
declare let v1!: number;
8080
declare var v2!: number;
81+
82+
declare namespace foo {
83+
var v!: number;
84+
}

0 commit comments

Comments
 (0)