Skip to content

Commit

Permalink
feat: downlevel template literal type in index signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Dec 2, 2022
1 parent c6cfd17 commit efcb682
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,25 @@ type T = [/** foo */ number, /** bar */ string];
The downlevel semantics are exactly the same as the original, but
the TypeScript language service won't be able to show the member names.
### `{ [key: T]: A }` (4.4)
Typescript 4.4 supports template literals as index signature types:
```ts
type O = { [key: `ak${string}`]: A };
```
becomes
```ts
type O = { [key: string]: A };
```
#### Semantics
The downlevel d.ts will be less strict because an index signature would
have a more general type (`string`) instead of the original template literal type.
### `in out T` (4.7)
Typescript 4.7 supports variance annotations on type parameter declarations:
Expand Down
3 changes: 3 additions & 0 deletions baselines/reference/ts3.4/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ export declare const foo: {
export type IR = IteratorResult<number>;
/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = string;
export type TTemplateLiteralIndexSignature = {
[key: string]: number;
};
3 changes: 3 additions & 0 deletions baselines/reference/ts3.5/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ export declare const foo: {
export type IR = IteratorResult<number>;
/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = string;
export type TTemplateLiteralIndexSignature = {
[key: string]: number;
};
3 changes: 3 additions & 0 deletions baselines/reference/ts3.6/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,6 @@ export declare const foo: {
export type IR = IteratorResult<number, string>;
/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = string;
export type TTemplateLiteralIndexSignature = {
[key: string]: number;
};
3 changes: 3 additions & 0 deletions baselines/reference/ts3.7/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,6 @@ export declare const foo: {
export type IR = IteratorResult<number, string>;
/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = string;
export type TTemplateLiteralIndexSignature = {
[key: string]: number;
};
3 changes: 3 additions & 0 deletions baselines/reference/ts3.8/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ export declare const foo: {
export type IR = IteratorResult<number, string>;
/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = string;
export type TTemplateLiteralIndexSignature = {
[key: string]: number;
};
3 changes: 3 additions & 0 deletions baselines/reference/ts3.9/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ export declare const foo: {
export type IR = IteratorResult<number, string>;
/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = string;
export type TTemplateLiteralIndexSignature = {
[key: string]: number;
};
3 changes: 3 additions & 0 deletions baselines/reference/ts4.0/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ export declare const foo: {
export type IR = IteratorResult<number, string>;
/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = string;
export type TTemplateLiteralIndexSignature = {
[key: string]: number;
};
3 changes: 3 additions & 0 deletions baselines/reference/ts4.1/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ export declare const foo: {
export type IR = IteratorResult<number, string>;
/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = `${string}abc${string}`;
export type TTemplateLiteralIndexSignature = {
[key: string]: number;
};
3 changes: 3 additions & 0 deletions baselines/reference/ts4.2/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ export declare const foo: {
export type IR = IteratorResult<number, string>;
/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = `${string}abc${string}`;
export type TTemplateLiteralIndexSignature = {
[key: string]: number;
};
3 changes: 3 additions & 0 deletions baselines/reference/ts4.3/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ export declare const foo: {
export type IR = IteratorResult<number, string>;
/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = `${string}abc${string}`;
export type TTemplateLiteralIndexSignature = {
[key: string]: number;
};
3 changes: 3 additions & 0 deletions baselines/reference/ts4.4/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ export declare const foo: {
export type IR = IteratorResult<number, string>;
/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = `${string}abc${string}`;
export type TTemplateLiteralIndexSignature = {
[key: `${string}abc${string}`]: number;
};
3 changes: 3 additions & 0 deletions baselines/reference/ts4.5/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ export declare const foo: {
export type IR = IteratorResult<number, string>;
/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = `${string}abc${string}`;
export type TTemplateLiteralIndexSignature = {
[key: `${string}abc${string}`]: number;
};
3 changes: 3 additions & 0 deletions baselines/reference/ts4.6/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ export declare const foo: {
export type IR = IteratorResult<number, string>;
/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = `${string}abc${string}`;
export type TTemplateLiteralIndexSignature = {
[key: `${string}abc${string}`]: number;
};
3 changes: 3 additions & 0 deletions baselines/reference/ts4.7/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ export declare const foo: {
export type IR = IteratorResult<number, string>;
/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = `${string}abc${string}`;
export type TTemplateLiteralIndexSignature = {
[key: `${string}abc${string}`]: number;
};
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,26 @@ function doTransform(checker, targetVersion, k) {
ts.unescapeLeadingUnderscores(member.name.escapedText),
/*hasTrailingNewline*/ false
);
} else if (
semver.lt(targetVersion, "4.4.0") &&
ts.isIndexSignatureDeclaration(n) &&
ts.SyntaxKind.TemplateLiteralType === n.parameters[0].type.kind
) {
const [p] = n.parameters;
return ts.factory.createIndexSignature(
n.modifiers,
[
ts.factory.createParameterDeclaration(
p.modifiers,
p.dotDotDotToken,
p.name,
p.questionToken,
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
p.initializer
)
],
n.type
);
} else if (semver.lt(targetVersion, "4.7.0") && ts.isTypeParameterDeclaration(n)) {
return ts.factory.createTypeParameterDeclaration(
n.modifiers?.filter(
Expand Down
2 changes: 2 additions & 0 deletions test/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,5 @@ export type IR = IteratorResult<number, string>;

/** Template Literal - supported since 4.1 < should be StringKeyword */
export type TTemplateLiteral = `${string}abc${string}`;

export type TTemplateLiteralIndexSignature = { [key: `${string}abc${string}`]: number }

0 comments on commit efcb682

Please sign in to comment.