Skip to content

Commit 825cde2

Browse files
authored
Merge pull request #30244 from Microsoft/declarationEmitError
Handle error when type parameter of mapped type uses private type
2 parents d2364f5 + fe9f424 commit 825cde2

7 files changed

+77
-0
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,6 +2927,10 @@
29272927
"category": "Error",
29282928
"code": 4102
29292929
},
2930+
"Type parameter '{0}' of exported mapped object type is using private name '{1}'.": {
2931+
"category": "Error",
2932+
"code": 4103
2933+
},
29302934

29312935
"The current host does not support the '{0}' option.": {
29322936
"category": "Error",

src/compiler/transformers/declarations/diagnostics.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@ namespace ts {
391391
diagnosticMessage = Diagnostics.Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1;
392392
break;
393393

394+
case SyntaxKind.MappedType:
395+
diagnosticMessage = Diagnostics.Type_parameter_0_of_exported_mapped_object_type_is_using_private_name_1;
396+
break;
397+
394398
case SyntaxKind.ConstructorType:
395399
case SyntaxKind.ConstructSignature:
396400
diagnosticMessage = Diagnostics.Type_parameter_0_of_constructor_signature_from_exported_interface_has_or_is_using_private_name_1;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/FromFactor.ts(2,15): error TS2304: Cannot find name 'StringKeyOf'.
2+
/FromFactor.ts(2,15): error TS4103: Type parameter 'TName' of exported mapped object type is using private name 'StringKeyOf'.
3+
4+
5+
==== /Helpers.ts (0 errors) ====
6+
export type StringKeyOf<TObj> = Extract<string, keyof TObj>;
7+
8+
==== /FromFactor.ts (2 errors) ====
9+
export type RowToColumns<TColumns> = {
10+
[TName in StringKeyOf<TColumns>]: any;
11+
~~~~~~~~~~~
12+
!!! error TS2304: Cannot find name 'StringKeyOf'.
13+
~~~~~~~~~~~
14+
!!! error TS4103: Type parameter 'TName' of exported mapped object type is using private name 'StringKeyOf'.
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [tests/cases/compiler/declarationEmitMappedPrivateTypeTypeParameter.ts] ////
2+
3+
//// [Helpers.ts]
4+
export type StringKeyOf<TObj> = Extract<string, keyof TObj>;
5+
6+
//// [FromFactor.ts]
7+
export type RowToColumns<TColumns> = {
8+
[TName in StringKeyOf<TColumns>]: any;
9+
}
10+
11+
//// [Helpers.js]
12+
"use strict";
13+
exports.__esModule = true;
14+
//// [FromFactor.js]
15+
"use strict";
16+
exports.__esModule = true;
17+
18+
19+
//// [Helpers.d.ts]
20+
export declare type StringKeyOf<TObj> = Extract<string, keyof TObj>;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== /Helpers.ts ===
2+
export type StringKeyOf<TObj> = Extract<string, keyof TObj>;
3+
>StringKeyOf : Symbol(StringKeyOf, Decl(Helpers.ts, 0, 0))
4+
>TObj : Symbol(TObj, Decl(Helpers.ts, 0, 24))
5+
>Extract : Symbol(Extract, Decl(lib.es5.d.ts, --, --))
6+
>TObj : Symbol(TObj, Decl(Helpers.ts, 0, 24))
7+
8+
=== /FromFactor.ts ===
9+
export type RowToColumns<TColumns> = {
10+
>RowToColumns : Symbol(RowToColumns, Decl(FromFactor.ts, 0, 0))
11+
>TColumns : Symbol(TColumns, Decl(FromFactor.ts, 0, 25))
12+
13+
[TName in StringKeyOf<TColumns>]: any;
14+
>TName : Symbol(TName, Decl(FromFactor.ts, 1, 5))
15+
>TColumns : Symbol(TColumns, Decl(FromFactor.ts, 0, 25))
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== /Helpers.ts ===
2+
export type StringKeyOf<TObj> = Extract<string, keyof TObj>;
3+
>StringKeyOf : Extract<string, keyof TObj>
4+
5+
=== /FromFactor.ts ===
6+
export type RowToColumns<TColumns> = {
7+
>RowToColumns : RowToColumns<TColumns>
8+
9+
[TName in StringKeyOf<TColumns>]: any;
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @declaration: true
2+
// @filename: /Helpers.ts
3+
export type StringKeyOf<TObj> = Extract<string, keyof TObj>;
4+
5+
// @filename: /FromFactor.ts
6+
export type RowToColumns<TColumns> = {
7+
[TName in StringKeyOf<TColumns>]: any;
8+
}

0 commit comments

Comments
 (0)