Skip to content

Commit e50305d

Browse files
committed
dts: don't emit type for private-identified field
Do not emit types for private-identified fields when generating declaration files. // example.ts export class A { #foo: number; } // example.d.ts export declare class A { #foo; } **This is not ideal!** The following would be better: // example.d.ts export declare unique class A { #foo; } See discussion: microsoft#33038 (comment)
1 parent b397afc commit e50305d

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

src/compiler/transformers/declarations.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ namespace ts {
461461
| PropertySignature;
462462

463463
function ensureType(node: HasInferredType, type: TypeNode | undefined, ignorePrivate?: boolean): TypeNode | undefined {
464-
if (!ignorePrivate && hasModifier(node, ModifierFlags.Private)) {
464+
if (!ignorePrivate && hasModifier(node, ModifierFlags.Private) || isNamedDeclaration(node) && isPrivateIdentifier(node.name)) {
465465
// Private nodes emit no types (except private parameter properties, whose parameter types are actually visible)
466466
return;
467467
}
@@ -898,7 +898,7 @@ namespace ts {
898898
ensureModifiers(input),
899899
input.name,
900900
input.questionToken,
901-
!hasModifier(input, ModifierFlags.Private) ? ensureType(input, input.type) : undefined,
901+
ensureType(input, input.type),
902902
ensureNoInitializer(input)
903903
));
904904
case SyntaxKind.PropertySignature:
@@ -907,7 +907,7 @@ namespace ts {
907907
ensureModifiers(input),
908908
input.name,
909909
input.questionToken,
910-
!hasModifier(input, ModifierFlags.Private) ? ensureType(input, input.type) : undefined,
910+
ensureType(input, input.type),
911911
ensureNoInitializer(input)
912912
));
913913
case SyntaxKind.MethodSignature: {
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
//// [privateNameDeclaration.ts]
2-
class A {
2+
export class A {
3+
// declaration file should not include the type of #name
34
#name: string;
45
}
56

67

78
//// [privateNameDeclaration.js]
9+
"use strict";
810
var _name;
11+
exports.__esModule = true;
912
var A = /** @class */ (function () {
1013
function A() {
14+
// declaration file should not include the type of #name
1115
_name.set(this, void 0);
1216
}
1317
return A;
1418
}());
19+
exports.A = A;
1520
_name = new WeakMap();
21+
22+
23+
//// [privateNameDeclaration.d.ts]
24+
export declare class A {
25+
#name;
26+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
=== tests/cases/conformance/classes/members/privateNames/privateNameDeclaration.ts ===
2-
class A {
2+
export class A {
33
>A : Symbol(A, Decl(privateNameDeclaration.ts, 0, 0))
44

5+
// declaration file should not include the type of #name
56
#name: string;
6-
>#name : Symbol(A.#name, Decl(privateNameDeclaration.ts, 0, 9))
7+
>#name : Symbol(A.#name, Decl(privateNameDeclaration.ts, 0, 16))
78
}
89

tests/baselines/reference/privateNameDeclaration.types

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
=== tests/cases/conformance/classes/members/privateNames/privateNameDeclaration.ts ===
2-
class A {
2+
export class A {
33
>A : A
44

5+
// declaration file should not include the type of #name
56
#name: string;
67
>#name : string
78
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
class A {
1+
// @declaration: true
2+
3+
export class A {
4+
// declaration file should not include the type of #name
25
#name: string;
36
}

0 commit comments

Comments
 (0)