UNPKG

type-to-string

Version:

Get a string representation of a TypeScript type.

80 lines (79 loc) 4.1 kB
import ts from "typescript"; import { Program, Type, Node, TypeNode } from "typescript"; /** * Get string representations of the given {@link Type}. */ declare function typeToString(program: Program, // eslint-disable-next-line functional/prefer-immutable-types type: Type, enclosingDeclaration?: ts.Node | undefined, flags?: ts.TypeFormatFlags): string; /** * Get the alias name of the given {@link Type}. */ declare function getTypeAliasAsString( // eslint-disable-next-line functional/prefer-immutable-types type: Type, withArguments?: boolean): string | null; /** * Get the name of the type reference on the given {@link Type}. * * If the given {@link Type} is not a type reference, `null` will be returned. */ declare function getTypeReferenceAsString(program: Program, // eslint-disable-next-line functional/prefer-immutable-types type: Type, withArguments?: boolean): string | null; /** * The same as {@link TypeFormatFlags} but with a few extra options added on. */ declare const TypeNodeFormatFlags: { readonly OmitTypeLiterals: number; readonly None: ts.TypeFormatFlags.None; readonly NoTruncation: ts.TypeFormatFlags.NoTruncation; readonly WriteArrayAsGenericType: ts.TypeFormatFlags.WriteArrayAsGenericType; readonly GenerateNamesForShadowedTypeParams: ts.TypeFormatFlags.GenerateNamesForShadowedTypeParams; readonly UseStructuralFallback: ts.TypeFormatFlags.UseStructuralFallback; readonly WriteTypeArgumentsOfSignature: ts.TypeFormatFlags.WriteTypeArgumentsOfSignature; readonly UseFullyQualifiedType: ts.TypeFormatFlags.UseFullyQualifiedType; readonly SuppressAnyReturnType: ts.TypeFormatFlags.SuppressAnyReturnType; readonly MultilineObjectLiterals: ts.TypeFormatFlags.MultilineObjectLiterals; readonly WriteClassExpressionAsTypeLiteral: ts.TypeFormatFlags.WriteClassExpressionAsTypeLiteral; readonly UseTypeOfFunction: ts.TypeFormatFlags.UseTypeOfFunction; readonly OmitParameterModifiers: ts.TypeFormatFlags.OmitParameterModifiers; readonly UseAliasDefinedOutsideCurrentScope: ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope; readonly UseSingleQuotesForStringLiteralType: ts.TypeFormatFlags.UseSingleQuotesForStringLiteralType; readonly NoTypeReduction: ts.TypeFormatFlags.NoTypeReduction; readonly OmitThisParameter: ts.TypeFormatFlags.OmitThisParameter; readonly AllowUniqueESSymbolType: ts.TypeFormatFlags.AllowUniqueESSymbolType; readonly AddUndefined: ts.TypeFormatFlags.AddUndefined; readonly WriteArrowStyleSignature: ts.TypeFormatFlags.WriteArrowStyleSignature; readonly InArrayType: ts.TypeFormatFlags.InArrayType; readonly InElementType: ts.TypeFormatFlags.InElementType; readonly InFirstTypeArgument: ts.TypeFormatFlags.InFirstTypeArgument; readonly InTypeAlias: ts.TypeFormatFlags.InTypeAlias; readonly NodeBuilderFlagsMask: ts.TypeFormatFlags.NodeBuilderFlagsMask; }; /** * Get the alias name of the given {@link TypeNode}. */ declare function getTypeNodeAliasAsString( // eslint-disable-next-line functional/prefer-immutable-types typeNode: TypeNode, withArguments?: boolean): string | null; /** * Get the name of the given {@link TypeReferenceNode}. * * If the given {@link Node} is not a {@link TypeReferenceNode}, `null` will be returned. */ declare function getTypeReferenceNodeAsString(program: Program, node: Node, withArguments?: boolean): string | null; /** * Get the {@link TypeNode} as written. */ declare function typeNodeAsWritten( // eslint-disable-next-line functional/prefer-immutable-types typeNode: TypeNode, keepAllWhiteSpace?: boolean): string; /** * Get string representations of the given {@link TypeNode}. * * @throws When the given type node is anonymous. */ declare function typeNodeToString(program: Program, // eslint-disable-next-line functional/prefer-immutable-types typeNode: TypeNode, flags?: number): string | null; export { getTypeAliasAsString, getTypeReferenceAsString, typeToString, TypeNodeFormatFlags, getTypeNodeAliasAsString, getTypeReferenceNodeAsString, typeNodeAsWritten, typeNodeToString };