Skip to content

Commit 5e898eb

Browse files
sstchursstchur-msftandrewbranch
authored
Expose getStringLiteralType and getNumberLiteralType on the TypeChecker, plus remove /** @internal */ from several useful methods. (#52473)
Co-authored-by: Stephen Stchur <[email protected]> Co-authored-by: Andrew Branch <[email protected]>
1 parent 9f8a160 commit 5e898eb

File tree

4 files changed

+111
-11
lines changed

4 files changed

+111
-11
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
17101710
createIndexInfo,
17111711
getAnyType: () => anyType,
17121712
getStringType: () => stringType,
1713+
getStringLiteralType,
17131714
getNumberType: () => numberType,
1715+
getNumberLiteralType,
1716+
getBigIntType: () => bigintType,
17141717
createPromiseType,
17151718
createArrayType,
17161719
getElementTypeOfArrayType,

src/compiler/types.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5099,17 +5099,46 @@ export interface TypeChecker {
50995099
getBaseConstraintOfType(type: Type): Type | undefined;
51005100
getDefaultFromTypeParameter(type: Type): Type | undefined;
51015101

5102-
/** @internal */ getAnyType(): Type;
5103-
/** @internal */ getStringType(): Type;
5104-
/** @internal */ getNumberType(): Type;
5105-
/** @internal */ getBooleanType(): Type;
5106-
/** @internal */ getFalseType(fresh?: boolean): Type;
5107-
/** @internal */ getTrueType(fresh?: boolean): Type;
5108-
/** @internal */ getVoidType(): Type;
5109-
/** @internal */ getUndefinedType(): Type;
5110-
/** @internal */ getNullType(): Type;
5111-
/** @internal */ getESSymbolType(): Type;
5112-
/** @internal */ getNeverType(): Type;
5102+
/**
5103+
* Gets the intrinsic `any` type. There are multiple types that act as `any` used internally in the compiler,
5104+
* so the type returned by this function should not be used in equality checks to determine if another type
5105+
* is `any`. Instead, use `type.flags & TypeFlags.Any`.
5106+
*/
5107+
getAnyType(): Type;
5108+
getStringType(): Type;
5109+
getStringLiteralType(value: string): StringLiteralType;
5110+
getNumberType(): Type;
5111+
getNumberLiteralType(value: number): NumberLiteralType;
5112+
getBigIntType(): Type;
5113+
getBooleanType(): Type;
5114+
/* eslint-disable @typescript-eslint/unified-signatures */
5115+
/** @internal */
5116+
getFalseType(fresh?: boolean): Type;
5117+
getFalseType(): Type;
5118+
/** @internal */
5119+
getTrueType(fresh?: boolean): Type;
5120+
getTrueType(): Type;
5121+
/* eslint-enable @typescript-eslint/unified-signatures */
5122+
getVoidType(): Type;
5123+
/**
5124+
* Gets the intrinsic `undefined` type. There are multiple types that act as `undefined` used internally in the compiler
5125+
* depending on compiler options, so the type returned by this function should not be used in equality checks to determine
5126+
* if another type is `undefined`. Instead, use `type.flags & TypeFlags.Undefined`.
5127+
*/
5128+
getUndefinedType(): Type;
5129+
/**
5130+
* Gets the intrinsic `null` type. There are multiple types that act as `null` used internally in the compiler,
5131+
* so the type returned by this function should not be used in equality checks to determine if another type
5132+
* is `null`. Instead, use `type.flags & TypeFlags.Null`.
5133+
*/
5134+
getNullType(): Type;
5135+
getESSymbolType(): Type;
5136+
/**
5137+
* Gets the intrinsic `never` type. There are multiple types that act as `never` used internally in the compiler,
5138+
* so the type returned by this function should not be used in equality checks to determine if another type
5139+
* is `never`. Instead, use `type.flags & TypeFlags.Never`.
5140+
*/
5141+
getNeverType(): Type;
51135142
/** @internal */ getOptionalType(): Type;
51145143
/** @internal */ getUnionType(types: Type[], subtypeReduction?: UnionReduction): Type;
51155144
/** @internal */ createArrayType(elementType: Type): Type;

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6369,6 +6369,40 @@ declare namespace ts {
63696369
getApparentType(type: Type): Type;
63706370
getBaseConstraintOfType(type: Type): Type | undefined;
63716371
getDefaultFromTypeParameter(type: Type): Type | undefined;
6372+
/**
6373+
* Gets the intrinsic `any` type. There are multiple types that act as `any` used internally in the compiler,
6374+
* so the type returned by this function should not be used in equality checks to determine if another type
6375+
* is `any`. Instead, use `type.flags & TypeFlags.Any`.
6376+
*/
6377+
getAnyType(): Type;
6378+
getStringType(): Type;
6379+
getStringLiteralType(value: string): StringLiteralType;
6380+
getNumberType(): Type;
6381+
getNumberLiteralType(value: number): NumberLiteralType;
6382+
getBigIntType(): Type;
6383+
getBooleanType(): Type;
6384+
getFalseType(): Type;
6385+
getTrueType(): Type;
6386+
getVoidType(): Type;
6387+
/**
6388+
* Gets the intrinsic `undefined` type. There are multiple types that act as `undefined` used internally in the compiler
6389+
* depending on compiler options, so the type returned by this function should not be used in equality checks to determine
6390+
* if another type is `undefined`. Instead, use `type.flags & TypeFlags.Undefined`.
6391+
*/
6392+
getUndefinedType(): Type;
6393+
/**
6394+
* Gets the intrinsic `null` type. There are multiple types that act as `null` used internally in the compiler,
6395+
* so the type returned by this function should not be used in equality checks to determine if another type
6396+
* is `null`. Instead, use `type.flags & TypeFlags.Null`.
6397+
*/
6398+
getNullType(): Type;
6399+
getESSymbolType(): Type;
6400+
/**
6401+
* Gets the intrinsic `never` type. There are multiple types that act as `never` used internally in the compiler,
6402+
* so the type returned by this function should not be used in equality checks to determine if another type
6403+
* is `never`. Instead, use `type.flags & TypeFlags.Never`.
6404+
*/
6405+
getNeverType(): Type;
63726406
/**
63736407
* True if this type is the `Array` or `ReadonlyArray` type from lib.d.ts.
63746408
* This function will _not_ return true if passed a type which

tests/baselines/reference/api/typescript.d.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,6 +2426,40 @@ declare namespace ts {
24262426
getApparentType(type: Type): Type;
24272427
getBaseConstraintOfType(type: Type): Type | undefined;
24282428
getDefaultFromTypeParameter(type: Type): Type | undefined;
2429+
/**
2430+
* Gets the intrinsic `any` type. There are multiple types that act as `any` used internally in the compiler,
2431+
* so the type returned by this function should not be used in equality checks to determine if another type
2432+
* is `any`. Instead, use `type.flags & TypeFlags.Any`.
2433+
*/
2434+
getAnyType(): Type;
2435+
getStringType(): Type;
2436+
getStringLiteralType(value: string): StringLiteralType;
2437+
getNumberType(): Type;
2438+
getNumberLiteralType(value: number): NumberLiteralType;
2439+
getBigIntType(): Type;
2440+
getBooleanType(): Type;
2441+
getFalseType(): Type;
2442+
getTrueType(): Type;
2443+
getVoidType(): Type;
2444+
/**
2445+
* Gets the intrinsic `undefined` type. There are multiple types that act as `undefined` used internally in the compiler
2446+
* depending on compiler options, so the type returned by this function should not be used in equality checks to determine
2447+
* if another type is `undefined`. Instead, use `type.flags & TypeFlags.Undefined`.
2448+
*/
2449+
getUndefinedType(): Type;
2450+
/**
2451+
* Gets the intrinsic `null` type. There are multiple types that act as `null` used internally in the compiler,
2452+
* so the type returned by this function should not be used in equality checks to determine if another type
2453+
* is `null`. Instead, use `type.flags & TypeFlags.Null`.
2454+
*/
2455+
getNullType(): Type;
2456+
getESSymbolType(): Type;
2457+
/**
2458+
* Gets the intrinsic `never` type. There are multiple types that act as `never` used internally in the compiler,
2459+
* so the type returned by this function should not be used in equality checks to determine if another type
2460+
* is `never`. Instead, use `type.flags & TypeFlags.Never`.
2461+
*/
2462+
getNeverType(): Type;
24292463
/**
24302464
* True if this type is the `Array` or `ReadonlyArray` type from lib.d.ts.
24312465
* This function will _not_ return true if passed a type which

0 commit comments

Comments
 (0)