-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20166 from Microsoft/definiteAssignmentAssertions
Definite assignment assertions
- Loading branch information
Showing
11 changed files
with
711 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
tests/baselines/reference/definiteAssignmentAssertions.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(5,5): error TS2564: Property 'b' has no initializer and is not definitely assigned in the constructor. | ||
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(20,6): error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(21,6): error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(22,13): error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(28,6): error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(34,15): error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(68,10): error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(69,10): error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(70,10): error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(75,15): error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts(76,15): error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
|
||
|
||
==== tests/cases/conformance/controlFlow/definiteAssignmentAssertions.ts (11 errors) ==== | ||
// Suppress strict property initialization check | ||
|
||
class C1 { | ||
a!: number; | ||
b: string; // Error | ||
~ | ||
!!! error TS2564: Property 'b' has no initializer and is not definitely assigned in the constructor. | ||
} | ||
|
||
// Suppress definite assignment check in constructor | ||
|
||
class C2 { | ||
a!: number; | ||
constructor() { | ||
let x = this.a; | ||
} | ||
} | ||
|
||
// Definite assignment assertion requires type annotation, no initializer, no static modifier | ||
|
||
class C3 { | ||
a! = 1; | ||
~ | ||
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
b!: number = 1; | ||
~ | ||
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
static c!: number; | ||
~ | ||
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
} | ||
|
||
// Definite assignment assertion not permitted in ambient context | ||
|
||
declare class C4 { | ||
a!: number; | ||
~ | ||
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
} | ||
|
||
// Definite assignment assertion not permitted on abstract property | ||
|
||
abstract class C5 { | ||
abstract a!: number; | ||
~ | ||
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
} | ||
|
||
// Suppress definite assignment check for variable | ||
|
||
function f1() { | ||
let x!: number; | ||
let y = x; | ||
var a!: number; | ||
var b = a; | ||
} | ||
|
||
function f2() { | ||
let x!: string | number; | ||
if (typeof x === "string") { | ||
let s: string = x; | ||
} | ||
else { | ||
let n: number = x; | ||
} | ||
} | ||
|
||
function f3() { | ||
let x!: number; | ||
const g = () => { | ||
x = 1; | ||
} | ||
g(); | ||
let y = x; | ||
} | ||
|
||
// Definite assignment assertion requires type annotation and no initializer | ||
|
||
function f4() { | ||
let a!; | ||
~ | ||
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
let b! = 1; | ||
~ | ||
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
let c!: number = 1; | ||
~ | ||
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
} | ||
|
||
// Definite assignment assertion not permitted in ambient context | ||
|
||
declare let v1!: number; | ||
~ | ||
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
declare var v2!: number; | ||
~ | ||
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context. | ||
|
Oops, something went wrong.