From aab7cb95fda4c5d8fcfc56a92cb83c45ef6ce7c9 Mon Sep 17 00:00:00 2001 From: Stephen Stchur Date: Fri, 27 Jan 2023 15:41:56 -0800 Subject: [PATCH 1/3] Expose getStringType, getStringLiteralType, getNumberType, getNumberLiteralType, getBooleanType, getFalseType, getTrueType, getUndefinedType, getNullType, getAnyType, getVoidType, getNeverType, and getESSymbolType on the TypeChecker --- src/compiler/checker.ts | 2 ++ src/compiler/types.ts | 24 ++++++++++--------- .../reference/api/tsserverlibrary.d.ts | 13 ++++++++++ tests/baselines/reference/api/typescript.d.ts | 13 ++++++++++ 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ca37ad9acf807..221517521aa27 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1701,7 +1701,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { createIndexInfo, getAnyType: () => anyType, getStringType: () => stringType, + getStringLiteralType, getNumberType: () => numberType, + getNumberLiteralType, createPromiseType, createArrayType, getElementTypeOfArrayType, diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 0a43afe978ea4..cb4812fd38129 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5099,17 +5099,19 @@ export interface TypeChecker { getBaseConstraintOfType(type: Type): Type | undefined; getDefaultFromTypeParameter(type: Type): Type | undefined; - /** @internal */ getAnyType(): Type; - /** @internal */ getStringType(): Type; - /** @internal */ getNumberType(): Type; - /** @internal */ getBooleanType(): Type; - /** @internal */ getFalseType(fresh?: boolean): Type; - /** @internal */ getTrueType(fresh?: boolean): Type; - /** @internal */ getVoidType(): Type; - /** @internal */ getUndefinedType(): Type; - /** @internal */ getNullType(): Type; - /** @internal */ getESSymbolType(): Type; - /** @internal */ getNeverType(): Type; + getAnyType(): Type; + getStringType(): Type; + getStringLiteralType(value: string): StringLiteralType; + getNumberType(): Type; + getNumberLiteralType(value: number): NumberLiteralType; + getBooleanType(): Type; + getFalseType(fresh?: boolean): Type; + getTrueType(fresh?: boolean): Type; + getVoidType(): Type; + getUndefinedType(): Type; + getNullType(): Type; + getESSymbolType(): Type; + getNeverType(): Type; /** @internal */ getOptionalType(): Type; /** @internal */ getUnionType(types: Type[], subtypeReduction?: UnionReduction): Type; /** @internal */ createArrayType(elementType: Type): Type; diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 65f9a35b41dc6..06dcf826fde08 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -6400,6 +6400,19 @@ declare namespace ts { getApparentType(type: Type): Type; getBaseConstraintOfType(type: Type): Type | undefined; getDefaultFromTypeParameter(type: Type): Type | undefined; + getAnyType(): Type; + getStringType(): Type; + getStringLiteralType(value: string): StringLiteralType; + getNumberType(): Type; + getNumberLiteralType(value: number): NumberLiteralType; + getBooleanType(): Type; + getFalseType(fresh?: boolean): Type; + getTrueType(fresh?: boolean): Type; + getVoidType(): Type; + getUndefinedType(): Type; + getNullType(): Type; + getESSymbolType(): Type; + getNeverType(): Type; getTypePredicateOfSignature(signature: Signature): TypePredicate | undefined; /** * Depending on the operation performed, it may be appropriate to throw away the checker diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index e973a18179380..3ec2adbb80ec7 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2425,6 +2425,19 @@ declare namespace ts { getApparentType(type: Type): Type; getBaseConstraintOfType(type: Type): Type | undefined; getDefaultFromTypeParameter(type: Type): Type | undefined; + getAnyType(): Type; + getStringType(): Type; + getStringLiteralType(value: string): StringLiteralType; + getNumberType(): Type; + getNumberLiteralType(value: number): NumberLiteralType; + getBooleanType(): Type; + getFalseType(fresh?: boolean): Type; + getTrueType(fresh?: boolean): Type; + getVoidType(): Type; + getUndefinedType(): Type; + getNullType(): Type; + getESSymbolType(): Type; + getNeverType(): Type; getTypePredicateOfSignature(signature: Signature): TypePredicate | undefined; /** * Depending on the operation performed, it may be appropriate to throw away the checker From 7d6158f241e7d8b5f245c0d062a1c5f3f9fd2740 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 3 Feb 2023 11:30:29 -0800 Subject: [PATCH 2/3] Update API baseline --- .../reference/api/tsserverlibrary.d.ts | 29 +++++++++++++++++++ tests/baselines/reference/api/typescript.d.ts | 29 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 65f9a35b41dc6..2b5871517763d 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -6310,6 +6310,7 @@ declare namespace ts { } interface TypeChecker { getTypeOfSymbolAtLocation(symbol: Symbol, node: Node): Type; + getTypeOfSymbol(symbol: Symbol): Type; getDeclaredTypeOfSymbol(symbol: Symbol): Type; getPropertiesOfType(type: Type): Symbol[]; getPropertyOfType(type: Type, propertyName: string): Symbol | undefined; @@ -6400,6 +6401,34 @@ declare namespace ts { getApparentType(type: Type): Type; getBaseConstraintOfType(type: Type): Type | undefined; getDefaultFromTypeParameter(type: Type): Type | undefined; + getAnyType(): Type; + getStringType(): Type; + getStringLiteralType(value: string): StringLiteralType; + getNumberType(): Type; + getNumberLiteralType(value: number): NumberLiteralType; + getBooleanType(): Type; + getFalseType(fresh?: boolean): Type; + getTrueType(fresh?: boolean): Type; + getVoidType(): Type; + getUndefinedType(): Type; + getNullType(): Type; + getESSymbolType(): Type; + getNeverType(): Type; + /** + * True if this type is the `Array` or `ReadonlyArray` type from lib.d.ts. + * This function will _not_ return true if passed a type which + * extends `Array` (for example, the TypeScript AST's `NodeArray` type). + */ + isArrayType(type: Type): boolean; + /** + * True if this type is a tuple type. This function will _not_ return true if + * passed a type which extends from a tuple. + */ + isTupleType(type: Type): boolean; + /** + * True if this type is assignable to `ReadonlyArray`. + */ + isArrayLikeType(type: Type): boolean; getTypePredicateOfSignature(signature: Signature): TypePredicate | undefined; /** * Depending on the operation performed, it may be appropriate to throw away the checker diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index e973a18179380..c222f27b223cc 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2335,6 +2335,7 @@ declare namespace ts { } interface TypeChecker { getTypeOfSymbolAtLocation(symbol: Symbol, node: Node): Type; + getTypeOfSymbol(symbol: Symbol): Type; getDeclaredTypeOfSymbol(symbol: Symbol): Type; getPropertiesOfType(type: Type): Symbol[]; getPropertyOfType(type: Type, propertyName: string): Symbol | undefined; @@ -2425,6 +2426,34 @@ declare namespace ts { getApparentType(type: Type): Type; getBaseConstraintOfType(type: Type): Type | undefined; getDefaultFromTypeParameter(type: Type): Type | undefined; + getAnyType(): Type; + getStringType(): Type; + getStringLiteralType(value: string): StringLiteralType; + getNumberType(): Type; + getNumberLiteralType(value: number): NumberLiteralType; + getBooleanType(): Type; + getFalseType(fresh?: boolean): Type; + getTrueType(fresh?: boolean): Type; + getVoidType(): Type; + getUndefinedType(): Type; + getNullType(): Type; + getESSymbolType(): Type; + getNeverType(): Type; + /** + * True if this type is the `Array` or `ReadonlyArray` type from lib.d.ts. + * This function will _not_ return true if passed a type which + * extends `Array` (for example, the TypeScript AST's `NodeArray` type). + */ + isArrayType(type: Type): boolean; + /** + * True if this type is a tuple type. This function will _not_ return true if + * passed a type which extends from a tuple. + */ + isTupleType(type: Type): boolean; + /** + * True if this type is assignable to `ReadonlyArray`. + */ + isArrayLikeType(type: Type): boolean; getTypePredicateOfSignature(signature: Signature): TypePredicate | undefined; /** * Depending on the operation performed, it may be appropriate to throw away the checker From d3cdc5898380ad8dba2e00b42a8508691800d6ac Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 1 Mar 2023 15:27:00 -0800 Subject: [PATCH 3/3] =?UTF-8?q?Don=E2=80=99t=20expose=20`fresh`=20paramete?= =?UTF-8?q?r,=20add=20`getBigIntType`,=20add=20JSDoc=20warnings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/compiler/checker.ts | 1 + src/compiler/types.ts | 27 +++++++++++++++++++ .../reference/api/tsserverlibrary.d.ts | 25 +++++++++++++++-- tests/baselines/reference/api/typescript.d.ts | 25 +++++++++++++++-- 4 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f40630bf97c19..49b148e18d570 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1706,6 +1706,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { getStringLiteralType, getNumberType: () => numberType, getNumberLiteralType, + getBigIntType: () => bigintType, createPromiseType, createArrayType, getElementTypeOfArrayType, diff --git a/src/compiler/types.ts b/src/compiler/types.ts index a42f4918ce7eb..abd82400fa1c4 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5099,18 +5099,45 @@ export interface TypeChecker { getBaseConstraintOfType(type: Type): Type | undefined; getDefaultFromTypeParameter(type: Type): Type | undefined; + /** + * Gets the intrinsic `any` type. There are multiple types that act as `any` used internally in the compiler, + * so the type returned by this function should not be used in equality checks to determine if another type + * is `any`. Instead, use `type.flags & TypeFlags.Any`. + */ getAnyType(): Type; getStringType(): Type; getStringLiteralType(value: string): StringLiteralType; getNumberType(): Type; getNumberLiteralType(value: number): NumberLiteralType; + getBigIntType(): Type; getBooleanType(): Type; + /* eslint-disable @typescript-eslint/unified-signatures */ + /** @internal */ getFalseType(fresh?: boolean): Type; + getFalseType(): Type; + /** @internal */ getTrueType(fresh?: boolean): Type; + getTrueType(): Type; + /* eslint-enable @typescript-eslint/unified-signatures */ getVoidType(): Type; + /** + * Gets the intrinsic `undefined` type. There are multiple types that act as `undefined` used internally in the compiler + * depending on compiler options, so the type returned by this function should not be used in equality checks to determine + * if another type is `undefined`. Instead, use `type.flags & TypeFlags.Undefined`. + */ getUndefinedType(): Type; + /** + * Gets the intrinsic `null` type. There are multiple types that act as `null` used internally in the compiler, + * so the type returned by this function should not be used in equality checks to determine if another type + * is `null`. Instead, use `type.flags & TypeFlags.Null`. + */ getNullType(): Type; getESSymbolType(): Type; + /** + * Gets the intrinsic `never` type. There are multiple types that act as `never` used internally in the compiler, + * so the type returned by this function should not be used in equality checks to determine if another type + * is `never`. Instead, use `type.flags & TypeFlags.Never`. + */ getNeverType(): Type; /** @internal */ getOptionalType(): Type; /** @internal */ getUnionType(types: Type[], subtypeReduction?: UnionReduction): Type; diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 2b5871517763d..3a322ee3083a4 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -6401,18 +6401,39 @@ declare namespace ts { getApparentType(type: Type): Type; getBaseConstraintOfType(type: Type): Type | undefined; getDefaultFromTypeParameter(type: Type): Type | undefined; + /** + * Gets the intrinsic `any` type. There are multiple types that act as `any` used internally in the compiler, + * so the type returned by this function should not be used in equality checks to determine if another type + * is `any`. Instead, use `type.flags & TypeFlags.Any`. + */ getAnyType(): Type; getStringType(): Type; getStringLiteralType(value: string): StringLiteralType; getNumberType(): Type; getNumberLiteralType(value: number): NumberLiteralType; + getBigIntType(): Type; getBooleanType(): Type; - getFalseType(fresh?: boolean): Type; - getTrueType(fresh?: boolean): Type; + getFalseType(): Type; + getTrueType(): Type; getVoidType(): Type; + /** + * Gets the intrinsic `undefined` type. There are multiple types that act as `undefined` used internally in the compiler + * depending on compiler options, so the type returned by this function should not be used in equality checks to determine + * if another type is `undefined`. Instead, use `type.flags & TypeFlags.Undefined`. + */ getUndefinedType(): Type; + /** + * Gets the intrinsic `null` type. There are multiple types that act as `null` used internally in the compiler, + * so the type returned by this function should not be used in equality checks to determine if another type + * is `null`. Instead, use `type.flags & TypeFlags.Null`. + */ getNullType(): Type; getESSymbolType(): Type; + /** + * Gets the intrinsic `never` type. There are multiple types that act as `never` used internally in the compiler, + * so the type returned by this function should not be used in equality checks to determine if another type + * is `never`. Instead, use `type.flags & TypeFlags.Never`. + */ getNeverType(): Type; /** * True if this type is the `Array` or `ReadonlyArray` type from lib.d.ts. diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index c222f27b223cc..46eed4e91b867 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2426,18 +2426,39 @@ declare namespace ts { getApparentType(type: Type): Type; getBaseConstraintOfType(type: Type): Type | undefined; getDefaultFromTypeParameter(type: Type): Type | undefined; + /** + * Gets the intrinsic `any` type. There are multiple types that act as `any` used internally in the compiler, + * so the type returned by this function should not be used in equality checks to determine if another type + * is `any`. Instead, use `type.flags & TypeFlags.Any`. + */ getAnyType(): Type; getStringType(): Type; getStringLiteralType(value: string): StringLiteralType; getNumberType(): Type; getNumberLiteralType(value: number): NumberLiteralType; + getBigIntType(): Type; getBooleanType(): Type; - getFalseType(fresh?: boolean): Type; - getTrueType(fresh?: boolean): Type; + getFalseType(): Type; + getTrueType(): Type; getVoidType(): Type; + /** + * Gets the intrinsic `undefined` type. There are multiple types that act as `undefined` used internally in the compiler + * depending on compiler options, so the type returned by this function should not be used in equality checks to determine + * if another type is `undefined`. Instead, use `type.flags & TypeFlags.Undefined`. + */ getUndefinedType(): Type; + /** + * Gets the intrinsic `null` type. There are multiple types that act as `null` used internally in the compiler, + * so the type returned by this function should not be used in equality checks to determine if another type + * is `null`. Instead, use `type.flags & TypeFlags.Null`. + */ getNullType(): Type; getESSymbolType(): Type; + /** + * Gets the intrinsic `never` type. There are multiple types that act as `never` used internally in the compiler, + * so the type returned by this function should not be used in equality checks to determine if another type + * is `never`. Instead, use `type.flags & TypeFlags.Never`. + */ getNeverType(): Type; /** * True if this type is the `Array` or `ReadonlyArray` type from lib.d.ts.