Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down
11 changes: 7 additions & 4 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just take a TypeParameter as input?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be the same as getBaseConstraintOfType. Which, BTW, getBaseConstraintOfType technically returns the resolvedBaseConstraint, while getConstraintFromTypeParameter would actually return the constraint; the different being that getBaseConstraintOfType follows all the constraints on your constraints to get the actual final constraint, while getConstraintFromTypeParameter is just the immediate constraint. Should we be exposing both?


/* @internal */ getAnyType(): Type;
/* @internal */ getStringType(): Type;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ namespace ts {
getNumberIndexType(): Type | undefined;
getBaseTypes(): BaseType[] | undefined;
getNonNullableType(): Type;
getConstraint(): Type | undefined;
getDefault(): Type | undefined;
}

export interface Signature {
Expand Down
7 changes: 4 additions & 3 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down