I would like to determine if one Type is assignable to another Type. So far I've only found the type comparison and checking functions in checker.ts, but that entire file is marked internal.
Is there a way to determine if one type is assignable to another using the exposed typescript library? A question to this effect on StackOverflow has not been answered. If not, could such functionality be added, say to TypeChecker?
this seems to be a rather basic change:
types.ts
export interface TypeChecker {
...
isTypeAssignableTo(source: Type, target: Type):boolean;
...
checker.ts
const checker: TypeChecker = {
...
isOptionalParameter,
isTypeAssignableTo
};
basic usage check:
let sourceFoo: ts.SourceFile = foo["source"];
let sourceBar: ts.SourceFile = bar["source"];
let sourceRay: ts.SourceFile = ray["source"];
let declFoo = <ts.ClassDeclaration>sourceFoo.statements.find(x => x.kind == ts.SyntaxKind.ClassDeclaration);
let declBar = <ts.ClassDeclaration>sourceBar.statements.find(x => x.kind == ts.SyntaxKind.ClassDeclaration);
let declRay = <ts.ClassDeclaration>sourceRay.statements.find(x => x.kind == ts.SyntaxKind.ClassDeclaration);
let checker = ref.typeChecker;
let typeFoo = checker.getTypeAtLocation(declFoo);
let typeBar = checker.getTypeAtLocation(declBar);
let typeRay = checker.getTypeAtLocation(declRay);
expect(checker.isTypeAssignableTo(typeFoo, typeFoo)).toBe(true);
expect(checker.isTypeAssignableTo(typeBar, typeBar)).toBe(true);
expect(checker.isTypeAssignableTo(typeFoo, typeBar)).toBe(true);
expect(checker.isTypeAssignableTo(typeFoo, typeRay)).toBe(false);
expect(checker.isTypeAssignableTo(typeBar, typeRay)).toBe(false);
with
export class Foo{ field:number = 10; } export class Bar{ field:number = 10; } export class Ray{ field:string }_alternatively_ expose isTypeRelatedTo instead
@RyanCavanaugh, @mhegazy any chance of a quick look to see if this is acceptable as Community Contribution at least?
We also had a request from a partner team to expose this and some other functions. This list looks fairly reasonable and shouldn't create any problems -- we'll be following up
isTypeAssignableTo
getAnyType
getUndefinedType
getBooleanType
getNullType
getNumberType
getStringType
createArrayType
getUnionType
@RyanCavanaugh would you allow me to PR a quick change to expose isTypeAssignableTo in TypeChecker, at least, or are you looking to make breaking changes before exposing this?
@RyanCavanaugh Also note, This specific issue is encompassed in #9879, which still has #9943 open.
@RyanCavanaugh any progress? This all seems to have stalled. All I really need is for the existing implementation of isTypeAssignableTo to be exposed - I'm happy to make a PR to that effect.
@RyanCavanaugh Same here (https://github.com/Microsoft/TypeScript/issues/19602), all we need is one function which lands with two lines of changes. Maybe you could add a Community label to this?
Marking as duplicate of https://github.com/Microsoft/TypeScript/issues/9879
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Most helpful comment
We also had a request from a partner team to expose this and some other functions. This list looks fairly reasonable and shouldn't create any problems -- we'll be following up