Skip to content

Commit a7ba54d

Browse files
committed
Add type relationship functions to checker api
1 parent 0eeb9cb commit a7ba54d

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

src/compiler/checker.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,39 @@ namespace ts {
103103

104104
getJsxElementAttributesType,
105105
getJsxIntrinsicTagNames,
106-
isOptionalParameter
106+
isOptionalParameter,
107+
108+
isIdenticalTo: (a, b) => checkTypeRelatedTo(a, b, identityRelation, /*errorNode*/undefined),
109+
isSubtypeOf: (a, b) => checkTypeRelatedTo(a, b, subtypeRelation, /*errorNode*/undefined),
110+
isAssignableTo: (a, b) => checkTypeRelatedTo(a, b, assignableRelation, /*errorNode*/undefined),
111+
isComparableTo: areTypesComparable,
112+
isInstantiationOf: (a, b) => {
113+
return a && b && (a.target === b);
114+
},
115+
116+
lookupGlobalType: name => {
117+
const symbol = getGlobalTypeSymbol(name);
118+
return symbol ? getDeclaredTypeOfSymbol(symbol) : unknownType;
119+
},
120+
lookupTypeAt: (name, node) => {
121+
const symbol = resolveName(node, name, SymbolFlags.Type, /*nameNotFoundMessage*/undefined, /*nameArg*/undefined);
122+
return symbol ? getDeclaredTypeOfSymbol(symbol) : unknownType;
123+
},
124+
125+
getAnyType: () => anyType,
126+
getStringType: () => stringType,
127+
getNumberType: () => numberType,
128+
getBooleanType: () => booleanType,
129+
getVoidType: () => voidType,
130+
getUndefinedType: () => undefinedType,
131+
getNullType: () => nullType,
132+
getESSymbolType: () => esSymbolType,
133+
getNeverType: () => neverType,
134+
getUnknownType: () => unknownType,
135+
getStringLiteralType: (text: string) => getLiteralTypeForText(TypeFlags.StringLiteral, text),
136+
getNumberLiteralType: (text: string) => getLiteralTypeForText(TypeFlags.NumberLiteral, text),
137+
getFalseType: () => falseType,
138+
getTrueType: () => falseType,
107139
};
108140

109141
const tupleTypes: Map<TupleType> = {};

src/compiler/types.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,86 @@ namespace ts {
18621862
getJsxIntrinsicTagNames(): Symbol[];
18631863
isOptionalParameter(node: ParameterDeclaration): boolean;
18641864

1865+
/**
1866+
* Two types are considered identical when
1867+
* - they are both the `any` type,
1868+
* - they are the same primitive type,
1869+
* - they are the same type parameter,
1870+
* - they are union types with identical sets of constituent types, or
1871+
* - they are intersection types with identical sets of constituent types, or
1872+
* - they are object types with identical sets of members.
1873+
*
1874+
* This relationship is bidirectional.
1875+
* See [here](https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.11.2) for more information.
1876+
*/
1877+
isIdenticalTo(a: Type, b: Type): boolean;
1878+
/**
1879+
* `a` is a ___subtype___ of `b` (and `b` is a ___supertype___ of `a`) if `a` has no excess properties with respect to `b`,
1880+
* and one of the following is true:
1881+
* - `a` and `b` are identical types.
1882+
* - `b` is the `any` type.
1883+
* - `a` is the `undefined` type.
1884+
* - `a` is the `null` type and `b` is _not_ the `undefined` type.
1885+
* - `a` is an enum type and `b` is the primitive type `number`.
1886+
* - `a` is a string literal type and `b` is the primitive type `string`.
1887+
* - `a` is a union type and each constituient type of `b` is a subtype of `b`.
1888+
* - `a` is an intersection type and at least one constituent type of `a` is a subtype of `b`.
1889+
* - `b` is a union type and `a` is a subtype of at least one constituent type of `b`.
1890+
* - `b` is an intersection type and `a` is a subtype of each constituent type of `b`.
1891+
* - `a` is a type parameter and the constraint of `a` is a subtype of `b`.
1892+
* - `a` has a subset of the structural members of `b`.
1893+
*
1894+
* This relationship is directional.
1895+
* See [here](https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.11.3) for more information.
1896+
*/
1897+
isSubtypeOf(a: Type, b: Type): boolean;
1898+
/**
1899+
* The assignable relationship differs only from the subtype relationship in that:
1900+
* - the `any` type is assignable to, but not a subtype of, all types
1901+
* - the primitive type `number` is assignable to, but not a subtype of, all enum types, and
1902+
* - an object type without a particular property is assignable to an object type in which that property is optional.
1903+
*
1904+
* This relationship is directional.
1905+
* See [here](https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.11.4) for more information.
1906+
*/
1907+
isAssignableTo(a: Type, b: Type): boolean;
1908+
/**
1909+
* True if `a` is assignable to `b`, or `b` is assignable to `a`. Additionally, all unions with
1910+
* overlapping constituient types are comparable, and unit types in the same domain are comparable.
1911+
* This relationship is bidirectional.
1912+
*/
1913+
isComparableTo(a: Type, b: Type): boolean;
1914+
/**
1915+
* Not a formal relationship - returns true if a is an instantiation of the generic type b
1916+
*/
1917+
isInstantiationOf(a: GenericType, b: GenericType): boolean;
1918+
1919+
/**
1920+
* Returns the declared type of the globally named symbol with meaning SymbolFlags.Type
1921+
* Returns the unknown type on failure.
1922+
*/
1923+
lookupGlobalType(name: string): Type;
1924+
/**
1925+
* Returns the declared type of the named symbol lexically at the position specified with meaning SymbolFlags.Type
1926+
* Returns the unknown type on failure.
1927+
*/
1928+
lookupTypeAt(name: string, position: Node): Type;
1929+
1930+
getAnyType(): Type;
1931+
getStringType(): Type;
1932+
getNumberType(): Type;
1933+
getBooleanType(): Type;
1934+
getVoidType(): Type;
1935+
getUndefinedType(): Type;
1936+
getNullType(): Type;
1937+
getESSymbolType(): Type;
1938+
getNeverType(): Type;
1939+
getUnknownType(): Type;
1940+
getStringLiteralType(text: string): LiteralType;
1941+
getNumberLiteralType(text: string): LiteralType;
1942+
getFalseType(): Type;
1943+
getTrueType(): Type;
1944+
18651945
// Should not be called directly. Should only be accessed through the Program instance.
18661946
/* @internal */ getDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
18671947
/* @internal */ getGlobalDiagnostics(): Diagnostic[];

0 commit comments

Comments
 (0)