-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support for defaulted type parameters
Reflect template parameter default values and use them as type arguments (#1348)
- Loading branch information
1 parent
96ede6a
commit f67f8db
Showing
10 changed files
with
164 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as ts from 'typescript'; | ||
|
||
/** | ||
* Returns the type parameters of a given type. | ||
* @param type The type whos type parameters are wanted. | ||
* @returns The type parameters of the type. An empty array if the type has no type parameters. | ||
*/ | ||
export function getTypeParametersOfType(type: ts.Type): ReadonlyArray<ts.TypeParameterDeclaration> { | ||
const declarations = type.getSymbol()?.getDeclarations() ?? []; | ||
|
||
for (const declaration of declarations) { | ||
if ((ts.isClassDeclaration(declaration) || ts.isInterfaceDeclaration(declaration)) && | ||
declaration.typeParameters) { | ||
return declaration.typeParameters; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* Returns a list of type arguments. If a type parameter has no corresponding type argument, the default type | ||
* for that type parameter is used as the type argument. | ||
* @param typeParams The type parameters for which the type arguments are wanted. | ||
* @param typeArguments The type arguments as provided in the declaration. | ||
* @returns The complete list of type arguments with possible default values if type arguments are missing. | ||
*/ | ||
export function getTypeArgumentsWithDefaults( | ||
typeParams: ts.TypeParameterDeclaration[], | ||
typeArguments?: ReadonlyArray<ts.TypeNode> | ||
): ReadonlyArray<ts.TypeNode> { | ||
if (!typeArguments || typeParams.length > typeArguments.length) { | ||
const typeArgumentsWithDefaults = new Array<ts.TypeNode>(); | ||
|
||
for (let i = 0; i < typeParams.length; ++i) { | ||
if (typeArguments && typeArguments[i]) { | ||
typeArgumentsWithDefaults.push(typeArguments[i]); | ||
} else { | ||
const defaultType = typeParams[i].default; | ||
|
||
if (defaultType) { | ||
typeArgumentsWithDefaults.push(defaultType); | ||
} | ||
} | ||
} | ||
|
||
return typeArgumentsWithDefaults; | ||
} | ||
|
||
return typeArguments; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.