diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ffb2b673210..cd4eeed7522 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -251,6 +251,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 53c36ac6caa..c7803c35ee0 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2771,7 +2771,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; @@ -3575,15 +3576,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 d62fc88a649..0d5e4680312 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 28de0566a50..e74b830cc45 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 0de484e36b5..c01647f7b4e 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -1783,6 +1783,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, @@ -2119,9 +2121,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; @@ -3843,6 +3842,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 810a16e6c6b..1e843eee60d 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -1783,6 +1783,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, @@ -2119,9 +2121,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; @@ -3843,6 +3842,8 @@ declare namespace ts { getNumberIndexType(): Type | undefined; getBaseTypes(): BaseType[] | undefined; getNonNullableType(): Type; + getConstraint(): Type | undefined; + getDefault(): Type | undefined; } interface Signature { getDeclaration(): SignatureDeclaration;