Skip to content

Commit 6a0bcfb

Browse files
committed
TS 5: Add support for const type parameters
1 parent aa36d85 commit 6a0bcfb

File tree

6 files changed

+32
-4
lines changed

6 files changed

+32
-4
lines changed

.config/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
../src/test/renderer/specs
99
../src/test/renderer/testProject
1010
../src/test/utils/options/readers/data/invalid2.json
11+
../src/test/converter2/behavior/constTypeParam.ts
1112
../static/main.js
1213
../example/docs/
1314
**/tmp

src/lib/converter/factories/signature.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,10 @@ function convertTypeParameters(
284284

285285
// There's no way to determine directly from a ts.TypeParameter what it's variance modifiers are
286286
// so unfortunately we have to go back to the node for this...
287-
const variance = getVariance(
288-
param.getSymbol()?.declarations?.find(ts.isTypeParameterDeclaration)
289-
?.modifiers
290-
);
287+
const declaration = param
288+
.getSymbol()
289+
?.declarations?.find(ts.isTypeParameterDeclaration);
290+
const variance = getVariance(declaration?.modifiers);
291291

292292
const paramRefl = new TypeParameterReflection(
293293
param.symbol.name,
@@ -296,6 +296,16 @@ function convertTypeParameters(
296296
parent,
297297
variance
298298
);
299+
300+
// No way to determine this from the type parameter itself, need to go back to the declaration
301+
if (
302+
declaration?.modifiers?.some(
303+
(m) => m.kind === ts.SyntaxKind.ConstKeyword
304+
)
305+
) {
306+
paramRefl.flags.setFlag(ReflectionFlag.Const, true);
307+
}
308+
299309
context.registerReflection(paramRefl, param.getSymbol());
300310
context.trigger(ConverterEvents.CREATE_TYPE_PARAMETER, paramRefl);
301311

@@ -329,6 +339,10 @@ export function createTypeParamReflection(
329339
context.scope,
330340
getVariance(param.modifiers)
331341
);
342+
if (param.modifiers?.some((m) => m.kind === ts.SyntaxKind.ConstKeyword)) {
343+
paramRefl.flags.setFlag(ReflectionFlag.Const, true);
344+
}
345+
332346
context.registerReflection(paramRefl, param.symbol);
333347

334348
if (ts.isJSDocTemplateTag(param.parent)) {

src/lib/output/themes/default/partials/typeParameters.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function typeParameters(context: DefaultThemeRenderContext, typeParameter
1111
{typeParameters?.map((item) => (
1212
<li>
1313
<h4>
14+
{item.flags.isConst && "const "}
1415
{item.varianceModifier ? `${item.varianceModifier} ` : ""}
1516
{item.name}
1617
{!!item.type && (

src/lib/output/themes/lib.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export function renderTypeParametersSignature(
9898
<span class="tsd-signature-symbol">{"<"}</span>
9999
{join(<span class="tsd-signature-symbol">{", "}</span>, typeParameters, (item) => (
100100
<>
101+
{item.flags.isConst && "const "}
101102
{item.varianceModifier ? `${item.varianceModifier} ` : ""}
102103
<span class="tsd-signature-type" data-tsd-kind={item.kindString}>
103104
{item.name}

src/test/behaviorTests.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ export const behaviorTests: {
142142
);
143143
},
144144

145+
constTypeParam(project) {
146+
const getNamesExactly = query(project, "getNamesExactly");
147+
const typeParams = getNamesExactly.signatures?.[0].typeParameters;
148+
equal(typeParams?.length, 1);
149+
equal(typeParams[0].flags.isConst, true);
150+
},
151+
145152
declareGlobal(project) {
146153
equal(
147154
project.children?.map((c) => c.name),
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export type HasNames = { names: readonly string[] };
2+
export function getNamesExactly<const T extends HasNames>(arg: T): T["names"] {
3+
return arg.names;
4+
}

0 commit comments

Comments
 (0)