diff --git a/internal/transformers/declarations/transform.go b/internal/transformers/declarations/transform.go index 07ff921521..02bb0d9d41 100644 --- a/internal/transformers/declarations/transform.go +++ b/internal/transformers/declarations/transform.go @@ -741,11 +741,16 @@ func (tx *DeclarationTransformer) transformPropertyDeclaration(input *ast.Proper if ast.IsPrivateIdentifier(input.Name()) { return nil } + // Remove definite assignment assertion (!) from declaration files + postfixToken := input.PostfixToken + if postfixToken != nil && postfixToken.Kind == ast.KindExclamationToken { + postfixToken = nil + } return tx.Factory().UpdatePropertyDeclaration( input, tx.ensureModifiers(input.AsNode()), input.Name(), - input.PostfixToken, + postfixToken, tx.ensureType(input.AsNode(), false), tx.ensureNoInitializer(input.AsNode()), ) diff --git a/testdata/baselines/reference/submodule/compiler/declarationEmitTypeofThisInClass.js b/testdata/baselines/reference/submodule/compiler/declarationEmitTypeofThisInClass.js index 513675497e..35209340b0 100644 --- a/testdata/baselines/reference/submodule/compiler/declarationEmitTypeofThisInClass.js +++ b/testdata/baselines/reference/submodule/compiler/declarationEmitTypeofThisInClass.js @@ -15,25 +15,6 @@ class Foo { //// [declarationEmitTypeofThisInClass.d.ts] declare class Foo { - foo!: string; - bar!: typeof this.foo; //Public property 'bar' of exported class has or is using private name 'this'.(4031) + foo: string; + bar: typeof this.foo; //Public property 'bar' of exported class has or is using private name 'this'.(4031) } - - -//// [DtsFileErrors] - - -declarationEmitTypeofThisInClass.d.ts(2,8): error TS1255: A definite assignment assertion '!' is not permitted in this context. -declarationEmitTypeofThisInClass.d.ts(3,8): error TS1255: A definite assignment assertion '!' is not permitted in this context. - - -==== declarationEmitTypeofThisInClass.d.ts (2 errors) ==== - declare class Foo { - foo!: string; - ~ -!!! error TS1255: A definite assignment assertion '!' is not permitted in this context. - bar!: typeof this.foo; //Public property 'bar' of exported class has or is using private name 'this'.(4031) - ~ -!!! error TS1255: A definite assignment assertion '!' is not permitted in this context. - } - \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/compiler/declarationEmitTypeofThisInClass.js.diff b/testdata/baselines/reference/submodule/compiler/declarationEmitTypeofThisInClass.js.diff index fd7f466383..4986e2b51c 100644 --- a/testdata/baselines/reference/submodule/compiler/declarationEmitTypeofThisInClass.js.diff +++ b/testdata/baselines/reference/submodule/compiler/declarationEmitTypeofThisInClass.js.diff @@ -13,27 +13,7 @@ //// [declarationEmitTypeofThisInClass.d.ts] declare class Foo { -- foo: string; + foo: string; - bar: typeof this.foo; -+ foo!: string; -+ bar!: typeof this.foo; //Public property 'bar' of exported class has or is using private name 'this'.(4031) - } -+ -+ -+//// [DtsFileErrors] -+ -+ -+declarationEmitTypeofThisInClass.d.ts(2,8): error TS1255: A definite assignment assertion '!' is not permitted in this context. -+declarationEmitTypeofThisInClass.d.ts(3,8): error TS1255: A definite assignment assertion '!' is not permitted in this context. -+ -+ -+==== declarationEmitTypeofThisInClass.d.ts (2 errors) ==== -+ declare class Foo { -+ foo!: string; -+ ~ -+!!! error TS1255: A definite assignment assertion '!' is not permitted in this context. -+ bar!: typeof this.foo; //Public property 'bar' of exported class has or is using private name 'this'.(4031) -+ ~ -+!!! error TS1255: A definite assignment assertion '!' is not permitted in this context. -+ } -+ \ No newline at end of file ++ bar: typeof this.foo; //Public property 'bar' of exported class has or is using private name 'this'.(4031) + } \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/compiler/initializerWithThisPropertyAccess.js b/testdata/baselines/reference/submodule/compiler/initializerWithThisPropertyAccess.js index 708b5b22f8..99824f7bfa 100644 --- a/testdata/baselines/reference/submodule/compiler/initializerWithThisPropertyAccess.js +++ b/testdata/baselines/reference/submodule/compiler/initializerWithThisPropertyAccess.js @@ -77,7 +77,7 @@ declare class B extends A { x: number; } declare class C { - a!: number; + a: number; b: number; } // Repro from #37979 diff --git a/testdata/baselines/reference/submodule/compiler/initializerWithThisPropertyAccess.js.diff b/testdata/baselines/reference/submodule/compiler/initializerWithThisPropertyAccess.js.diff index deddbe076d..3815821550 100644 --- a/testdata/baselines/reference/submodule/compiler/initializerWithThisPropertyAccess.js.diff +++ b/testdata/baselines/reference/submodule/compiler/initializerWithThisPropertyAccess.js.diff @@ -56,12 +56,8 @@ c: () => number; d: number; constructor(); -@@= skipped -46, +41 lines =@@ - x: number; - } - declare class C { -- a: number; -+ a!: number; +@@= skipped -49, +44 lines =@@ + a: number; b: number; } +// Repro from #37979 diff --git a/testdata/baselines/reference/submodule/conformance/definiteAssignmentAssertions.js b/testdata/baselines/reference/submodule/conformance/definiteAssignmentAssertions.js index 6693863b4c..13029babed 100644 --- a/testdata/baselines/reference/submodule/conformance/definiteAssignmentAssertions.js +++ b/testdata/baselines/reference/submodule/conformance/definiteAssignmentAssertions.js @@ -143,28 +143,28 @@ function f4() { //// [definiteAssignmentAssertions.d.ts] // Suppress strict property initialization check declare class C1 { - a!: number; + a: number; b: string; // Error } // Suppress definite assignment check in constructor declare class C2 { - a!: number; + a: number; constructor(); } // Definite assignment assertion requires type annotation, no initializer, no static modifier declare class C3 { - a!: number; - b!: number; - static c!: number; - d!: any; + a: number; + b: number; + static c: number; + d: any; } // Definite assignment assertion not permitted in ambient context declare class C4 { - a!: number; + a: number; } // Definite assignment assertion not permitted on abstract property declare abstract class C5 { - abstract a!: number; + abstract a: number; } // Suppress definite assignment check for variable declare function f1(): void; diff --git a/testdata/baselines/reference/submodule/conformance/definiteAssignmentAssertions.js.diff b/testdata/baselines/reference/submodule/conformance/definiteAssignmentAssertions.js.diff index fe69e1efc0..ad6b1a8640 100644 --- a/testdata/baselines/reference/submodule/conformance/definiteAssignmentAssertions.js.diff +++ b/testdata/baselines/reference/submodule/conformance/definiteAssignmentAssertions.js.diff @@ -40,37 +40,29 @@ //// [definiteAssignmentAssertions.d.ts] +// Suppress strict property initialization check declare class C1 { -- a: number; + a: number; - b: string; -+ a!: number; + b: string; // Error } +// Suppress definite assignment check in constructor declare class C2 { -- a: number; -+ a!: number; + a: number; constructor(); } +// Definite assignment assertion requires type annotation, no initializer, no static modifier declare class C3 { -- a: number; -- b: number; -- static c: number; -- d: any; -+ a!: number; -+ b!: number; -+ static c!: number; -+ d!: any; + a: number; + b: number; + static c: number; + d: any; } +// Definite assignment assertion not permitted in ambient context declare class C4 { -- a: number; -+ a!: number; + a: number; } +// Definite assignment assertion not permitted on abstract property declare abstract class C5 { -- abstract a: number; -+ abstract a!: number; + abstract a: number; } +// Suppress definite assignment check for variable declare function f1(): void; diff --git a/testdata/baselines/reference/submodule/conformance/importEqualsDeclaration.js b/testdata/baselines/reference/submodule/conformance/importEqualsDeclaration.js index 99a3a5b35c..cc1037f80e 100644 --- a/testdata/baselines/reference/submodule/conformance/importEqualsDeclaration.js +++ b/testdata/baselines/reference/submodule/conformance/importEqualsDeclaration.js @@ -40,7 +40,7 @@ void type; // Ok //// [a.d.ts] declare class A { - a!: string; + a: string; } export = A; //// [b.d.ts] diff --git a/testdata/baselines/reference/submodule/conformance/importEqualsDeclaration.js.diff b/testdata/baselines/reference/submodule/conformance/importEqualsDeclaration.js.diff index bd64ec0e30..639e20e004 100644 --- a/testdata/baselines/reference/submodule/conformance/importEqualsDeclaration.js.diff +++ b/testdata/baselines/reference/submodule/conformance/importEqualsDeclaration.js.diff @@ -17,16 +17,7 @@ A.prototype; // Error const a = { a: 'a' }; // Ok void type; // Ok -@@= skipped -8, +8 lines =@@ - - //// [a.d.ts] - declare class A { -- a: string; -+ a!: string; - } - export = A; - //// [b.d.ts] -@@= skipped -8, +8 lines =@@ +@@= skipped -16, +16 lines =@@ } export = SomeClass; //// [c.d.ts]