Skip to content

Commit 00315a5

Browse files
Copilotjakebailey
andauthored
Fix: Remove definite assignment assertions from class property declaration files (#1751)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jakebailey <[email protected]>
1 parent 563c3ff commit 00315a5

9 files changed

+32
-87
lines changed

internal/transformers/declarations/transform.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,11 +741,16 @@ func (tx *DeclarationTransformer) transformPropertyDeclaration(input *ast.Proper
741741
if ast.IsPrivateIdentifier(input.Name()) {
742742
return nil
743743
}
744+
// Remove definite assignment assertion (!) from declaration files
745+
postfixToken := input.PostfixToken
746+
if postfixToken != nil && postfixToken.Kind == ast.KindExclamationToken {
747+
postfixToken = nil
748+
}
744749
return tx.Factory().UpdatePropertyDeclaration(
745750
input,
746751
tx.ensureModifiers(input.AsNode()),
747752
input.Name(),
748-
input.PostfixToken,
753+
postfixToken,
749754
tx.ensureType(input.AsNode(), false),
750755
tx.ensureNoInitializer(input.AsNode()),
751756
)

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

testdata/baselines/reference/submodule/conformance/definiteAssignmentAssertions.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,28 +143,28 @@ function f4() {
143143
//// [definiteAssignmentAssertions.d.ts]
144144
// Suppress strict property initialization check
145145
declare class C1 {
146-
a!: number;
146+
a: number;
147147
b: string; // Error
148148
}
149149
// Suppress definite assignment check in constructor
150150
declare class C2 {
151-
a!: number;
151+
a: number;
152152
constructor();
153153
}
154154
// Definite assignment assertion requires type annotation, no initializer, no static modifier
155155
declare class C3 {
156-
a!: number;
157-
b!: number;
158-
static c!: number;
159-
d!: any;
156+
a: number;
157+
b: number;
158+
static c: number;
159+
d: any;
160160
}
161161
// Definite assignment assertion not permitted in ambient context
162162
declare class C4 {
163-
a!: number;
163+
a: number;
164164
}
165165
// Definite assignment assertion not permitted on abstract property
166166
declare abstract class C5 {
167-
abstract a!: number;
167+
abstract a: number;
168168
}
169169
// Suppress definite assignment check for variable
170170
declare function f1(): void;

testdata/baselines/reference/submodule/conformance/definiteAssignmentAssertions.js.diff

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,37 +40,29 @@
4040
//// [definiteAssignmentAssertions.d.ts]
4141
+// Suppress strict property initialization check
4242
declare class C1 {
43-
- a: number;
43+
a: number;
4444
- b: string;
45-
+ a!: number;
4645
+ b: string; // Error
4746
}
4847
+// Suppress definite assignment check in constructor
4948
declare class C2 {
50-
- a: number;
51-
+ a!: number;
49+
a: number;
5250
constructor();
5351
}
5452
+// Definite assignment assertion requires type annotation, no initializer, no static modifier
5553
declare class C3 {
56-
- a: number;
57-
- b: number;
58-
- static c: number;
59-
- d: any;
60-
+ a!: number;
61-
+ b!: number;
62-
+ static c!: number;
63-
+ d!: any;
54+
a: number;
55+
b: number;
56+
static c: number;
57+
d: any;
6458
}
6559
+// Definite assignment assertion not permitted in ambient context
6660
declare class C4 {
67-
- a: number;
68-
+ a!: number;
61+
a: number;
6962
}
7063
+// Definite assignment assertion not permitted on abstract property
7164
declare abstract class C5 {
72-
- abstract a: number;
73-
+ abstract a!: number;
65+
abstract a: number;
7466
}
7567
+// Suppress definite assignment check for variable
7668
declare function f1(): void;

testdata/baselines/reference/submodule/conformance/importEqualsDeclaration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void type; // Ok
4040

4141
//// [a.d.ts]
4242
declare class A {
43-
a!: string;
43+
a: string;
4444
}
4545
export = A;
4646
//// [b.d.ts]

testdata/baselines/reference/submodule/conformance/importEqualsDeclaration.js.diff

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,7 @@
1717
A.prototype; // Error
1818
const a = { a: 'a' }; // Ok
1919
void type; // Ok
20-
@@= skipped -8, +8 lines =@@
21-
22-
//// [a.d.ts]
23-
declare class A {
24-
- a: string;
25-
+ a!: string;
26-
}
27-
export = A;
28-
//// [b.d.ts]
29-
@@= skipped -8, +8 lines =@@
20+
@@= skipped -16, +16 lines =@@
3021
}
3122
export = SomeClass;
3223
//// [c.d.ts]

0 commit comments

Comments
 (0)