Skip to content

Commit f0d5b10

Browse files
authoredMar 30, 2023
feat: actually prohibit use of generic types (#79)
The documentation says you can't use them, but the compiler never actually checked, and it silently ignored them (possibly reducing the type references to the "apparent type"). --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
1 parent a3dc983 commit f0d5b10

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed
 

‎src/assembler.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,11 @@ export class Assembler implements Emitter {
10561056
);
10571057
}
10581058

1059+
const classDeclaration = type.symbol.valueDeclaration as ts.ClassDeclaration;
1060+
for (const typeParam of classDeclaration.typeParameters ?? []) {
1061+
this._diagnostics.push(JsiiDiagnostic.JSII_1006_GENERIC_TYPE.create(typeParam));
1062+
}
1063+
10591064
const jsiiType: spec.ClassType = bindings.setClassRelatedNode(
10601065
{
10611066
assembly: this.projectInfo.name,
@@ -1065,7 +1070,7 @@ export class Assembler implements Emitter {
10651070
namespace: ctx.namespace.length > 0 ? ctx.namespace.join('.') : undefined,
10661071
docs: this._visitDocumentation(type.symbol, ctx).docs,
10671072
},
1068-
type.symbol.valueDeclaration as ts.ClassDeclaration,
1073+
classDeclaration,
10691074
);
10701075

10711076
if (_isAbstract(type.symbol, jsiiType)) {
@@ -1675,6 +1680,10 @@ export class Assembler implements Emitter {
16751680
| ts.InterfaceDeclaration
16761681
| undefined;
16771682

1683+
for (const typeParam of typeDecl?.typeParameters ?? []) {
1684+
this._diagnostics.push(JsiiDiagnostic.JSII_1006_GENERIC_TYPE.create(typeParam));
1685+
}
1686+
16781687
for (const decl of (typeDecl?.members as ReadonlyArray<ts.ClassElement | ts.TypeElement> | undefined)?.filter(
16791688
(mem) => ts.isIndexSignatureDeclaration(mem),
16801689
) ?? []) {

‎src/jsii-diagnostic.ts

+6
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ export class JsiiDiagnostic implements ts.Diagnostic {
286286
name: 'typescript-restrictions/separate-write-type',
287287
});
288288

289+
public static readonly JSII_1006_GENERIC_TYPE = Code.error({
290+
code: 1006,
291+
formatter: () => 'Generic types are not supported because semantics are not uniform in target languages.',
292+
name: 'typescript-restriction/generic-type',
293+
});
294+
289295
public static readonly JSII_1999_UNSUPPORTED = Code.error({
290296
code: 1999,
291297
formatter: ({

‎test/__snapshots__/negatives.test.ts.snap

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/negatives/neg.generics.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export class GenericClass<T extends object> {
2+
private constructor() { }
3+
4+
public retrieveGeneric(): T {
5+
return {} as any;
6+
}
7+
}
8+
9+
export interface GenericStruct<T> {
10+
readonly generic: T;
11+
}
12+
13+
export interface IGenericBehavior<T extends object | string> {
14+
genericMethod(param: T): void;
15+
}

0 commit comments

Comments
 (0)
Please sign in to comment.