diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cde61bb4071e7..8eb6f2b13f475 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -250,6 +250,7 @@ namespace ts { getSuggestionForNonexistentProperty: (node, type) => getSuggestionForNonexistentProperty(node, type), getSuggestionForNonexistentSymbol: (location, name, meaning) => getSuggestionForNonexistentSymbol(location, escapeLeadingUnderscores(name), meaning), getBaseConstraintOfType, + getDefaultFromTypeParameter: type => type && type.flags & TypeFlags.TypeParameter ? getDefaultFromTypeParameter(type as TypeParameter) : undefined, resolveName(name, location, meaning) { return resolveName(location, escapeLeadingUnderscores(name), meaning, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ false); }, diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 26148a0b2f191..82f66e7727871 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2766,7 +2766,8 @@ namespace ts { getApparentType(type: Type): Type; getSuggestionForNonexistentProperty(node: Identifier, containingType: Type): string | undefined; getSuggestionForNonexistentSymbol(location: Node, name: string, meaning: SymbolFlags): string | undefined; - /* @internal */ getBaseConstraintOfType(type: Type): Type | undefined; + getBaseConstraintOfType(type: Type): Type | undefined; + getDefaultFromTypeParameter(type: Type): Type | undefined; /* @internal */ getAnyType(): Type; /* @internal */ getStringType(): Type; @@ -3570,15 +3571,17 @@ namespace ts { export interface TypeVariable extends Type { /* @internal */ - resolvedBaseConstraint: Type; + resolvedBaseConstraint?: Type; /* @internal */ - resolvedIndexType: IndexType; + resolvedIndexType?: IndexType; } // Type parameters (TypeFlags.TypeParameter) export interface TypeParameter extends TypeVariable { /** Retrieve using getConstraintFromTypeParameter */ - constraint: Type; // Constraint + /* @internal */ + constraint?: Type; // Constraint + /* @internal */ default?: Type; /* @internal */ target?: TypeParameter; // Instantiation target diff --git a/src/services/services.ts b/src/services/services.ts index d62fc88a649d0..0d5e468031231 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -472,6 +472,12 @@ namespace ts { getNonNullableType(): Type { return this.checker.getNonNullableType(this); } + getConstraint(): Type | undefined { + return this.checker.getBaseConstraintOfType(this); + } + getDefault(): Type | undefined { + return this.checker.getDefaultFromTypeParameter(this); + } } class SignatureObject implements Signature { diff --git a/src/services/types.ts b/src/services/types.ts index 28de0566a500b..e74b830cc45c9 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -50,6 +50,8 @@ namespace ts { getNumberIndexType(): Type | undefined; getBaseTypes(): BaseType[] | undefined; getNonNullableType(): Type; + getConstraint(): Type | undefined; + getDefault(): Type | undefined; } export interface Signature { diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index d3a97c756902b..d322e04893e0c 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -1779,6 +1779,8 @@ declare namespace ts { getApparentType(type: Type): Type; getSuggestionForNonexistentProperty(node: Identifier, containingType: Type): string | undefined; getSuggestionForNonexistentSymbol(location: Node, name: string, meaning: SymbolFlags): string | undefined; + getBaseConstraintOfType(type: Type): Type | undefined; + getDefaultFromTypeParameter(type: Type): Type | undefined; } enum NodeBuilderFlags { None = 0, @@ -2115,9 +2117,6 @@ declare namespace ts { interface TypeVariable extends Type { } interface TypeParameter extends TypeVariable { - /** Retrieve using getConstraintFromTypeParameter */ - constraint: Type; - default?: Type; } interface IndexedAccessType extends TypeVariable { objectType: Type; @@ -3838,6 +3837,8 @@ declare namespace ts { getNumberIndexType(): Type | undefined; getBaseTypes(): BaseType[] | undefined; getNonNullableType(): Type; + getConstraint(): Type | undefined; + getDefault(): Type | undefined; } interface Signature { getDeclaration(): SignatureDeclaration; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 43b668fe2d1a1..17c75ed0336f1 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -1779,6 +1779,8 @@ declare namespace ts { getApparentType(type: Type): Type; getSuggestionForNonexistentProperty(node: Identifier, containingType: Type): string | undefined; getSuggestionForNonexistentSymbol(location: Node, name: string, meaning: SymbolFlags): string | undefined; + getBaseConstraintOfType(type: Type): Type | undefined; + getDefaultFromTypeParameter(type: Type): Type | undefined; } enum NodeBuilderFlags { None = 0, @@ -2115,9 +2117,6 @@ declare namespace ts { interface TypeVariable extends Type { } interface TypeParameter extends TypeVariable { - /** Retrieve using getConstraintFromTypeParameter */ - constraint: Type; - default?: Type; } interface IndexedAccessType extends TypeVariable { objectType: Type; @@ -3838,6 +3837,8 @@ declare namespace ts { getNumberIndexType(): Type | undefined; getBaseTypes(): BaseType[] | undefined; getNonNullableType(): Type; + getConstraint(): Type | undefined; + getDefault(): Type | undefined; } interface Signature { getDeclaration(): SignatureDeclaration;