Skip to content

Commit dc8cf0d

Browse files
committed
Add type relationship functions to checker api
1 parent 2fbc225 commit dc8cf0d

File tree

5 files changed

+345
-0
lines changed

5 files changed

+345
-0
lines changed

Jakefile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ var harnessSources = harnessCoreSources.concat([
123123
"reuseProgramStructure.ts",
124124
"textStorage.ts",
125125
"cachingInServerLSHost.ts",
126+
"checkerPublicRelationships.ts",
126127
"moduleResolution.ts",
127128
"tsconfigParsing.ts",
128129
"commandLineParsing.ts",

src/compiler/checker.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,59 @@ namespace ts {
233233
return resolveName(location, escapeLeadingUnderscores(name), meaning, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
234234
},
235235
getJsxNamespace: () => unescapeLeadingUnderscores(getJsxNamespace()),
236+
237+
isIdenticalTo: (a, b) => checkTypeRelatedTo(a, b, identityRelation, /*errorNode*/ undefined),
238+
isSubtypeOf: (a, b) => checkTypeRelatedTo(a, b, subtypeRelation, /*errorNode*/ undefined),
239+
isAssignableTo: (a, b) => checkTypeRelatedTo(a, b, assignableRelation, /*errorNode*/ undefined),
240+
isComparableTo: areTypesComparable,
241+
isInstantiationOf: (a, b) => {
242+
return a && b && (a.target === b);
243+
},
244+
245+
lookupGlobalType: name => {
246+
const symbol = getSymbol(globals, escapeLeadingUnderscores(name), SymbolFlags.Type);
247+
return symbol ? getDeclaredTypeOfSymbol(symbol) : unknownType;
248+
},
249+
lookupGlobalValueType: name => {
250+
const symbol = getSymbol(globals, escapeLeadingUnderscores(name), SymbolFlags.Value);
251+
return symbol ? getTypeOfSymbol(symbol) : unknownType;
252+
},
253+
lookupTypeAt: (name, node) => {
254+
const symbol = resolveName(node, escapeLeadingUnderscores(name), SymbolFlags.Type, /*nameNotFoundMessage*/undefined, /*nameArg*/undefined);
255+
return symbol ? getDeclaredTypeOfSymbol(symbol) : unknownType;
256+
},
257+
lookupValueTypeAt: (name, node) => {
258+
const symbol = resolveName(node, escapeLeadingUnderscores(name), SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/undefined);
259+
return symbol ? getTypeOfSymbol(symbol) : unknownType;
260+
},
261+
getTypeOfSymbol,
262+
263+
getAnyType: () => anyType,
264+
getStringType: () => stringType,
265+
getNumberType: () => numberType,
266+
getBooleanType: () => booleanType,
267+
getVoidType: () => voidType,
268+
getUndefinedType: () => undefinedType,
269+
getNullType: () => nullType,
270+
getESSymbolType: () => esSymbolType,
271+
getNeverType: () => neverType,
272+
getUnknownType: () => unknownType,
273+
getStringLiteralType: (text: string) => {
274+
/* tslint:disable:no-null-keyword */
275+
Debug.assert(text !== undefined && text !== null);
276+
/* tslint:enable:no-null-keyword */
277+
Debug.assert(typeof text === "string");
278+
return getLiteralType(text);
279+
},
280+
getNumberLiteralType: (number: number) => {
281+
/* tslint:disable:no-null-keyword */
282+
Debug.assert(number !== undefined && number !== null);
283+
/* tslint:enable:no-null-keyword */
284+
Debug.assert(typeof number === "number");
285+
return getLiteralType(number);
286+
},
287+
getFalseType: () => falseType,
288+
getTrueType: () => trueType,
236289
};
237290

238291
const tupleTypes: GenericType[] = [];

src/compiler/types.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,8 +2686,103 @@ namespace ts {
26862686

26872687
/* @internal */ tryFindAmbientModuleWithoutAugmentations(moduleName: string): Symbol | undefined;
26882688

2689+
26892690
/* @internal */ getSymbolWalker(accept?: (symbol: Symbol) => boolean): SymbolWalker;
26902691

2692+
/**
2693+
* Two types are considered identical when
2694+
* - they are both the `any` type,
2695+
* - they are the same primitive type,
2696+
* - they are the same type parameter,
2697+
* - they are union types with identical sets of constituent types, or
2698+
* - they are intersection types with identical sets of constituent types, or
2699+
* - they are object types with identical sets of members.
2700+
*
2701+
* This relationship is bidirectional.
2702+
* See [here](https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.11.2) for more information.
2703+
*/
2704+
isIdenticalTo(a: Type, b: Type): boolean;
2705+
/**
2706+
* `a` is a ___subtype___ of `b` (and `b` is a ___supertype___ of `a`) if `a` has no excess properties with respect to `b`,
2707+
* and one of the following is true:
2708+
* - `a` and `b` are identical types.
2709+
* - `b` is the `any` type.
2710+
* - `a` is the `undefined` type.
2711+
* - `a` is the `null` type and `b` is _not_ the `undefined` type.
2712+
* - `a` is an enum type and `b` is the primitive type `number`.
2713+
* - `a` is a string literal type and `b` is the primitive type `string`.
2714+
* - `a` is a union type and each constituient type of `b` is a subtype of `b`.
2715+
* - `a` is an intersection type and at least one constituent type of `a` is a subtype of `b`.
2716+
* - `b` is a union type and `a` is a subtype of at least one constituent type of `b`.
2717+
* - `b` is an intersection type and `a` is a subtype of each constituent type of `b`.
2718+
* - `a` is a type parameter and the constraint of `a` is a subtype of `b`.
2719+
* - `a` has a subset of the structural members of `b`.
2720+
*
2721+
* This relationship is directional.
2722+
* See [here](https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.11.3) for more information.
2723+
*/
2724+
isSubtypeOf(a: Type, b: Type): boolean;
2725+
/**
2726+
* The assignable relationship differs only from the subtype relationship in that:
2727+
* - the `any` type is assignable to, but not a subtype of, all types
2728+
* - the primitive type `number` is assignable to, but not a subtype of, all enum types, and
2729+
* - an object type without a particular property is assignable to an object type in which that property is optional.
2730+
*
2731+
* This relationship is directional.
2732+
* See [here](https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.11.4) for more information.
2733+
*/
2734+
isAssignableTo(a: Type, b: Type): boolean;
2735+
/**
2736+
* True if `a` is assignable to `b`, or `b` is assignable to `a`. Additionally, all unions with
2737+
* overlapping constituient types are comparable, and unit types in the same domain are comparable.
2738+
* This relationship is bidirectional.
2739+
*/
2740+
isComparableTo(a: Type, b: Type): boolean;
2741+
/**
2742+
* Not a formal relationship - returns true if a is an instantiation of the generic type b
2743+
*/
2744+
isInstantiationOf(a: GenericType, b: GenericType): boolean;
2745+
2746+
/**
2747+
* Returns the declared type of the globally named symbol with meaning SymbolFlags.Type
2748+
* Returns the unknown type on failure.
2749+
*/
2750+
lookupGlobalType(name: string): Type;
2751+
/**
2752+
* Returns the declared type of the globally named symbol with meaning SymbolFlags.Value
2753+
* Returns the unknown type on failure.
2754+
*/
2755+
lookupGlobalValueType(name: string): Type;
2756+
/**
2757+
* Returns the declared type of the named symbol lexically at the position specified with meaning SymbolFlags.Type
2758+
* Returns the unknown type on failure.
2759+
*/
2760+
lookupTypeAt(name: string, position: Node): Type;
2761+
/**
2762+
* Returns the declared type of the named symbol lexically at the position specified with meaning SymbolFlags.Value
2763+
* Returns the unknown type on failure.
2764+
*/
2765+
lookupValueTypeAt(name: string, position: Node): Type;
2766+
/**
2767+
* Returns the type of a symbol
2768+
*/
2769+
getTypeOfSymbol(symbol: Symbol): Type;
2770+
2771+
getAnyType(): Type;
2772+
getStringType(): Type;
2773+
getNumberType(): Type;
2774+
getBooleanType(): Type;
2775+
getVoidType(): Type;
2776+
getUndefinedType(): Type;
2777+
getNullType(): Type;
2778+
getESSymbolType(): Type;
2779+
getNeverType(): Type;
2780+
getUnknownType(): Type;
2781+
getStringLiteralType(text: string): LiteralType;
2782+
getNumberLiteralType(num: number): LiteralType;
2783+
getFalseType(): Type;
2784+
getTrueType(): Type;
2785+
26912786
// Should not be called directly. Should only be accessed through the Program instance.
26922787
/* @internal */ getDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
26932788
/* @internal */ getGlobalDiagnostics(): Diagnostic[];

src/harness/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"./unittests/transpile.ts",
111111
"./unittests/reuseProgramStructure.ts",
112112
"./unittests/cachingInServerLSHost.ts",
113+
"./unittests/checkerPublicRelationships.ts",
113114
"./unittests/moduleResolution.ts",
114115
"./unittests/tsconfigParsing.ts",
115116
"./unittests/commandLineParsing.ts",

0 commit comments

Comments
 (0)