Skip to content

Commit 439394b

Browse files
committed
Add type relationship functions to checker api
1 parent 2a6aacd commit 439394b

File tree

5 files changed

+344
-0
lines changed

5 files changed

+344
-0
lines changed

Jakefile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ var harnessSources = harnessCoreSources.concat([
121121
"reuseProgramStructure.ts",
122122
"textStorage.ts",
123123
"cachingInServerLSHost.ts",
124+
"checkerPublicRelationships.ts",
124125
"moduleResolution.ts",
125126
"tsconfigParsing.ts",
126127
"commandLineParsing.ts",

src/compiler/checker.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,59 @@ namespace ts {
227227
location = getParseTreeNode(location);
228228
return resolveName(location, name, meaning, /*nameNotFoundMessage*/ undefined, name);
229229
},
230+
231+
isIdenticalTo: (a, b) => checkTypeRelatedTo(a, b, identityRelation, /*errorNode*/ undefined),
232+
isSubtypeOf: (a, b) => checkTypeRelatedTo(a, b, subtypeRelation, /*errorNode*/ undefined),
233+
isAssignableTo: (a, b) => checkTypeRelatedTo(a, b, assignableRelation, /*errorNode*/ undefined),
234+
isComparableTo: areTypesComparable,
235+
isInstantiationOf: (a, b) => {
236+
return a && b && (a.target === b);
237+
},
238+
239+
lookupGlobalType: name => {
240+
const symbol = getSymbol(globals, name, SymbolFlags.Type);
241+
return symbol ? getDeclaredTypeOfSymbol(symbol) : unknownType;
242+
},
243+
lookupGlobalValueType: name => {
244+
const symbol = getSymbol(globals, name, SymbolFlags.Value);
245+
return symbol ? getTypeOfSymbol(symbol) : unknownType;
246+
},
247+
lookupTypeAt: (name, node) => {
248+
const symbol = resolveName(node, name, SymbolFlags.Type, /*nameNotFoundMessage*/undefined, /*nameArg*/undefined);
249+
return symbol ? getDeclaredTypeOfSymbol(symbol) : unknownType;
250+
},
251+
lookupValueTypeAt: (name, node) => {
252+
const symbol = resolveName(node, name, SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/undefined);
253+
return symbol ? getTypeOfSymbol(symbol) : unknownType;
254+
},
255+
getTypeOfSymbol,
256+
257+
getAnyType: () => anyType,
258+
getStringType: () => stringType,
259+
getNumberType: () => numberType,
260+
getBooleanType: () => booleanType,
261+
getVoidType: () => voidType,
262+
getUndefinedType: () => undefinedType,
263+
getNullType: () => nullType,
264+
getESSymbolType: () => esSymbolType,
265+
getNeverType: () => neverType,
266+
getUnknownType: () => unknownType,
267+
getStringLiteralType: (text: string) => {
268+
/* tslint:disable:no-null-keyword */
269+
Debug.assert(text !== undefined && text !== null);
270+
/* tslint:enable:no-null-keyword */
271+
return getLiteralTypeForText(TypeFlags.StringLiteral, "" + text);
272+
},
273+
getNumberLiteralType: (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" || typeof text === "number"); // While not formally part of the function signature, allow coercions from numbers
278+
return getLiteralTypeForText(TypeFlags.NumberLiteral, "" + text);
279+
},
280+
getFalseType: () => falseType,
281+
getTrueType: () => trueType,
282+
>>>>>>> Add type relationship functions to checker api
230283
};
231284

232285
const tupleTypes: GenericType[] = [];

src/compiler/types.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,6 +2629,100 @@ namespace ts {
26292629

26302630
/* @internal */ tryFindAmbientModuleWithoutAugmentations(moduleName: string): Symbol | undefined;
26312631

2632+
/**
2633+
* Two types are considered identical when
2634+
* - they are both the `any` type,
2635+
* - they are the same primitive type,
2636+
* - they are the same type parameter,
2637+
* - they are union types with identical sets of constituent types, or
2638+
* - they are intersection types with identical sets of constituent types, or
2639+
* - they are object types with identical sets of members.
2640+
*
2641+
* This relationship is bidirectional.
2642+
* See [here](https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.11.2) for more information.
2643+
*/
2644+
isIdenticalTo(a: Type, b: Type): boolean;
2645+
/**
2646+
* `a` is a ___subtype___ of `b` (and `b` is a ___supertype___ of `a`) if `a` has no excess properties with respect to `b`,
2647+
* and one of the following is true:
2648+
* - `a` and `b` are identical types.
2649+
* - `b` is the `any` type.
2650+
* - `a` is the `undefined` type.
2651+
* - `a` is the `null` type and `b` is _not_ the `undefined` type.
2652+
* - `a` is an enum type and `b` is the primitive type `number`.
2653+
* - `a` is a string literal type and `b` is the primitive type `string`.
2654+
* - `a` is a union type and each constituient type of `b` is a subtype of `b`.
2655+
* - `a` is an intersection type and at least one constituent type of `a` is a subtype of `b`.
2656+
* - `b` is a union type and `a` is a subtype of at least one constituent type of `b`.
2657+
* - `b` is an intersection type and `a` is a subtype of each constituent type of `b`.
2658+
* - `a` is a type parameter and the constraint of `a` is a subtype of `b`.
2659+
* - `a` has a subset of the structural members of `b`.
2660+
*
2661+
* This relationship is directional.
2662+
* See [here](https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.11.3) for more information.
2663+
*/
2664+
isSubtypeOf(a: Type, b: Type): boolean;
2665+
/**
2666+
* The assignable relationship differs only from the subtype relationship in that:
2667+
* - the `any` type is assignable to, but not a subtype of, all types
2668+
* - the primitive type `number` is assignable to, but not a subtype of, all enum types, and
2669+
* - an object type without a particular property is assignable to an object type in which that property is optional.
2670+
*
2671+
* This relationship is directional.
2672+
* See [here](https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.11.4) for more information.
2673+
*/
2674+
isAssignableTo(a: Type, b: Type): boolean;
2675+
/**
2676+
* True if `a` is assignable to `b`, or `b` is assignable to `a`. Additionally, all unions with
2677+
* overlapping constituient types are comparable, and unit types in the same domain are comparable.
2678+
* This relationship is bidirectional.
2679+
*/
2680+
isComparableTo(a: Type, b: Type): boolean;
2681+
/**
2682+
* Not a formal relationship - returns true if a is an instantiation of the generic type b
2683+
*/
2684+
isInstantiationOf(a: GenericType, b: GenericType): boolean;
2685+
2686+
/**
2687+
* Returns the declared type of the globally named symbol with meaning SymbolFlags.Type
2688+
* Returns the unknown type on failure.
2689+
*/
2690+
lookupGlobalType(name: string): Type;
2691+
/**
2692+
* Returns the declared type of the globally named symbol with meaning SymbolFlags.Value
2693+
* Returns the unknown type on failure.
2694+
*/
2695+
lookupGlobalValueType(name: string): Type;
2696+
/**
2697+
* Returns the declared type of the named symbol lexically at the position specified with meaning SymbolFlags.Type
2698+
* Returns the unknown type on failure.
2699+
*/
2700+
lookupTypeAt(name: string, position: Node): Type;
2701+
/**
2702+
* Returns the declared type of the named symbol lexically at the position specified with meaning SymbolFlags.Value
2703+
* Returns the unknown type on failure.
2704+
*/
2705+
lookupValueTypeAt(name: string, position: Node): Type;
2706+
/**
2707+
* Returns the type of a symbol
2708+
*/
2709+
getTypeOfSymbol(symbol: Symbol): Type;
2710+
2711+
getAnyType(): Type;
2712+
getStringType(): Type;
2713+
getNumberType(): Type;
2714+
getBooleanType(): Type;
2715+
getVoidType(): Type;
2716+
getUndefinedType(): Type;
2717+
getNullType(): Type;
2718+
getESSymbolType(): Type;
2719+
getNeverType(): Type;
2720+
getUnknownType(): Type;
2721+
getStringLiteralType(text: string): LiteralType;
2722+
getNumberLiteralType(text: string): LiteralType;
2723+
getFalseType(): Type;
2724+
getTrueType(): Type;
2725+
26322726
// Should not be called directly. Should only be accessed through the Program instance.
26332727
/* @internal */ getDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
26342728
/* @internal */ getGlobalDiagnostics(): Diagnostic[];

src/harness/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
"./unittests/transpile.ts",
114114
"./unittests/reuseProgramStructure.ts",
115115
"./unittests/cachingInServerLSHost.ts",
116+
"./unittests/checkerPublicRelationships.ts",
116117
"./unittests/moduleResolution.ts",
117118
"./unittests/tsconfigParsing.ts",
118119
"./unittests/commandLineParsing.ts",

0 commit comments

Comments
 (0)