Skip to content

Commit 0025519

Browse files
Copilotjakebailey
andcommitted
Fix: Remove definite assignment assertions from declaration files
Co-authored-by: jakebailey <[email protected]>
1 parent 53d08bf commit 0025519

15 files changed

+190
-104
lines changed

internal/transformers/declarations/transform.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,11 +727,18 @@ func (tx *DeclarationTransformer) transformPropertySignatureDeclaration(input *a
727727
if ast.IsPrivateIdentifier(input.Name()) {
728728
return nil
729729
}
730+
// Remove definite assignment assertion (!) in declaration files
731+
var postfixToken *ast.TokenNode
732+
if input.PostfixToken != nil && input.PostfixToken.Kind == ast.KindExclamationToken {
733+
postfixToken = nil
734+
} else {
735+
postfixToken = input.PostfixToken
736+
}
730737
return tx.Factory().UpdatePropertySignatureDeclaration(
731738
input,
732739
tx.ensureModifiers(input.AsNode()),
733740
input.Name(),
734-
input.PostfixToken,
741+
postfixToken,
735742
tx.ensureType(input.AsNode(), false),
736743
tx.ensureNoInitializer(input.AsNode()), // TODO: possible strada bug (fixed here) - const property signatures never initialized
737744
)
@@ -741,11 +748,18 @@ func (tx *DeclarationTransformer) transformPropertyDeclaration(input *ast.Proper
741748
if ast.IsPrivateIdentifier(input.Name()) {
742749
return nil
743750
}
751+
// Remove definite assignment assertion (!) in declaration files
752+
var postfixToken *ast.TokenNode
753+
if input.PostfixToken != nil && input.PostfixToken.Kind == ast.KindExclamationToken {
754+
postfixToken = nil
755+
} else {
756+
postfixToken = input.PostfixToken
757+
}
744758
return tx.Factory().UpdatePropertyDeclaration(
745759
input,
746760
tx.ensureModifiers(input.AsNode()),
747761
input.Name(),
748-
input.PostfixToken,
762+
postfixToken,
749763
tx.ensureType(input.AsNode(), false),
750764
tx.ensureNoInitializer(input.AsNode()),
751765
)

testdata/baselines/reference/compiler/definiteAssignmentAssertionInDeclaration.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,5 @@ exports.DbObject = DbObject;
1717

1818
//// [definiteAssignmentAssertionInDeclaration.d.ts]
1919
export declare class DbObject {
20-
id!: string;
20+
id: string;
2121
}
22-
23-
24-
//// [DtsFileErrors]
25-
26-
27-
definiteAssignmentAssertionInDeclaration.d.ts(2,7): error TS1255: A definite assignment assertion '!' is not permitted in this context.
28-
29-
30-
==== definiteAssignmentAssertionInDeclaration.d.ts (1 errors) ====
31-
export declare class DbObject {
32-
id!: string;
33-
~
34-
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context.
35-
}
36-
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
definiteAssignmentAssertionInDeclarationExtended.ts(7,18): error TS1255: A definite assignment assertion '!' is not permitted in this context.
2+
3+
4+
==== definiteAssignmentAssertionInDeclarationExtended.ts (1 errors) ====
5+
export class DbObject {
6+
id!: string;
7+
name?: string;
8+
count: number = 0;
9+
private secret!: string;
10+
protected value!: number;
11+
static config!: boolean;
12+
~
13+
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context.
14+
}
15+
16+
export interface IConfig {
17+
setting?: boolean;
18+
optionalSetting?: string;
19+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [tests/cases/compiler/definiteAssignmentAssertionInDeclarationExtended.ts] ////
2+
3+
//// [definiteAssignmentAssertionInDeclarationExtended.ts]
4+
export class DbObject {
5+
id!: string;
6+
name?: string;
7+
count: number = 0;
8+
private secret!: string;
9+
protected value!: number;
10+
static config!: boolean;
11+
}
12+
13+
export interface IConfig {
14+
setting?: boolean;
15+
optionalSetting?: string;
16+
}
17+
18+
//// [definiteAssignmentAssertionInDeclarationExtended.js]
19+
"use strict";
20+
Object.defineProperty(exports, "__esModule", { value: true });
21+
exports.DbObject = void 0;
22+
class DbObject {
23+
id;
24+
name;
25+
count = 0;
26+
secret;
27+
value;
28+
static config;
29+
}
30+
exports.DbObject = DbObject;
31+
32+
33+
//// [definiteAssignmentAssertionInDeclarationExtended.d.ts]
34+
export declare class DbObject {
35+
id: string;
36+
name?: string;
37+
count: number;
38+
private secret;
39+
protected value: number;
40+
static config: boolean;
41+
}
42+
export interface IConfig {
43+
setting?: boolean;
44+
optionalSetting?: string;
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [tests/cases/compiler/definiteAssignmentAssertionInDeclarationExtended.ts] ////
2+
3+
=== definiteAssignmentAssertionInDeclarationExtended.ts ===
4+
export class DbObject {
5+
>DbObject : Symbol(DbObject, Decl(definiteAssignmentAssertionInDeclarationExtended.ts, 0, 0))
6+
7+
id!: string;
8+
>id : Symbol(DbObject.id, Decl(definiteAssignmentAssertionInDeclarationExtended.ts, 0, 23))
9+
10+
name?: string;
11+
>name : Symbol(DbObject.name, Decl(definiteAssignmentAssertionInDeclarationExtended.ts, 1, 16))
12+
13+
count: number = 0;
14+
>count : Symbol(DbObject.count, Decl(definiteAssignmentAssertionInDeclarationExtended.ts, 2, 18))
15+
16+
private secret!: string;
17+
>secret : Symbol(DbObject.secret, Decl(definiteAssignmentAssertionInDeclarationExtended.ts, 3, 22))
18+
19+
protected value!: number;
20+
>value : Symbol(DbObject.value, Decl(definiteAssignmentAssertionInDeclarationExtended.ts, 4, 28))
21+
22+
static config!: boolean;
23+
>config : Symbol(DbObject.config, Decl(definiteAssignmentAssertionInDeclarationExtended.ts, 5, 29))
24+
}
25+
26+
export interface IConfig {
27+
>IConfig : Symbol(IConfig, Decl(definiteAssignmentAssertionInDeclarationExtended.ts, 7, 1))
28+
29+
setting?: boolean;
30+
>setting : Symbol(IConfig.setting, Decl(definiteAssignmentAssertionInDeclarationExtended.ts, 9, 26))
31+
32+
optionalSetting?: string;
33+
>optionalSetting : Symbol(IConfig.optionalSetting, Decl(definiteAssignmentAssertionInDeclarationExtended.ts, 10, 22))
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [tests/cases/compiler/definiteAssignmentAssertionInDeclarationExtended.ts] ////
2+
3+
=== definiteAssignmentAssertionInDeclarationExtended.ts ===
4+
export class DbObject {
5+
>DbObject : DbObject
6+
7+
id!: string;
8+
>id : string
9+
10+
name?: string;
11+
>name : string | undefined
12+
13+
count: number = 0;
14+
>count : number
15+
>0 : 0
16+
17+
private secret!: string;
18+
>secret : string
19+
20+
protected value!: number;
21+
>value : number
22+
23+
static config!: boolean;
24+
>config : boolean
25+
}
26+
27+
export interface IConfig {
28+
setting?: boolean;
29+
>setting : boolean | undefined
30+
31+
optionalSetting?: string;
32+
>optionalSetting : string | undefined
33+
}

testdata/baselines/reference/submodule/compiler/declarationEmitTypeofThisInClass.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,6 @@ class Foo {
1515

1616
//// [declarationEmitTypeofThisInClass.d.ts]
1717
declare class Foo {
18-
foo!: string;
19-
bar!: typeof this.foo; //Public property 'bar' of exported class has or is using private name 'this'.(4031)
18+
foo: string;
19+
bar: typeof this.foo; //Public property 'bar' of exported class has or is using private name 'this'.(4031)
2020
}
21-
22-
23-
//// [DtsFileErrors]
24-
25-
26-
declarationEmitTypeofThisInClass.d.ts(2,8): error TS1255: A definite assignment assertion '!' is not permitted in this context.
27-
declarationEmitTypeofThisInClass.d.ts(3,8): error TS1255: A definite assignment assertion '!' is not permitted in this context.
28-
29-
30-
==== declarationEmitTypeofThisInClass.d.ts (2 errors) ====
31-
declare class Foo {
32-
foo!: string;
33-
~
34-
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context.
35-
bar!: typeof this.foo; //Public property 'bar' of exported class has or is using private name 'this'.(4031)
36-
~
37-
!!! error TS1255: A definite assignment assertion '!' is not permitted in this context.
38-
}
39-

testdata/baselines/reference/submodule/compiler/declarationEmitTypeofThisInClass.js.diff

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,7 @@
1313

1414
//// [declarationEmitTypeofThisInClass.d.ts]
1515
declare class Foo {
16-
- foo: string;
16+
foo: string;
1717
- bar: typeof this.foo;
18-
+ foo!: string;
19-
+ bar!: typeof this.foo; //Public property 'bar' of exported class has or is using private name 'this'.(4031)
20-
}
21-
+
22-
+
23-
+//// [DtsFileErrors]
24-
+
25-
+
26-
+declarationEmitTypeofThisInClass.d.ts(2,8): error TS1255: A definite assignment assertion '!' is not permitted in this context.
27-
+declarationEmitTypeofThisInClass.d.ts(3,8): error TS1255: A definite assignment assertion '!' is not permitted in this context.
28-
+
29-
+
30-
+==== declarationEmitTypeofThisInClass.d.ts (2 errors) ====
31-
+ declare class Foo {
32-
+ foo!: string;
33-
+ ~
34-
+!!! error TS1255: A definite assignment assertion '!' is not permitted in this context.
35-
+ bar!: typeof this.foo; //Public property 'bar' of exported class has or is using private name 'this'.(4031)
36-
+ ~
37-
+!!! error TS1255: A definite assignment assertion '!' is not permitted in this context.
38-
+ }
39-
+
18+
+ bar: typeof this.foo; //Public property 'bar' of exported class has or is using private name 'this'.(4031)
19+
}

testdata/baselines/reference/submodule/compiler/initializerWithThisPropertyAccess.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ declare class B extends A {
7777
x: number;
7878
}
7979
declare class C {
80-
a!: number;
80+
a: number;
8181
b: number;
8282
}
8383
// Repro from #37979

testdata/baselines/reference/submodule/compiler/initializerWithThisPropertyAccess.js.diff

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,8 @@
5656
c: () => number;
5757
d: number;
5858
constructor();
59-
@@= skipped -46, +41 lines =@@
60-
x: number;
61-
}
62-
declare class C {
63-
- a: number;
64-
+ a!: number;
59+
@@= skipped -49, +44 lines =@@
60+
a: number;
6561
b: number;
6662
}
6763
+// Repro from #37979

0 commit comments

Comments
 (0)