Repository navigation
Expose isTypeAssignableTo in TypeChecker? #11728
Description
Activity
MeirionHughes commented
on Oct 28, 2016 AuthorMore actionsthis 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
isTypeRelatedToinsteadRyan Cavanaugh (@RyanCavanaugh), Mohamed Hegazy (@mhegazy) any chance of a quick look to see if this is acceptable as Community Contribution at least?
- addedDiscussionIssues which may not have code impactIssues which may not have code impact
on Oct 28, 2016 RyanCavanaugh commented
on Oct 28, 2016 MemberMore actionsWe 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
Reacted by Meirion Hughes, Rob Eisenberg and Kirill AgalakovMeirionHughes commented
on Nov 17, 2016 AuthorMore actionsRyan Cavanaugh (@RyanCavanaugh) would you allow me to PR a quick change to expose
isTypeAssignableToinTypeChecker, at least, or are you looking to make breaking changes before exposing this?Ryan Cavanaugh (@RyanCavanaugh) Also note, This specific issue is encompassed in #9879, which still has #9943 open.
Reacted by Meirion HughesReacted by Meirion HughesMeirionHughes commented
on Apr 10, 2017 AuthorMore actionsRyan Cavanaugh (@RyanCavanaugh) any progress? This all seems to have stalled. All I really need is for the existing implementation of
isTypeAssignableToto be exposed - I'm happy to make a PR to that effect.Ryan Cavanaugh (@RyanCavanaugh) Same here (#19602), all we need is one function which lands with two lines of changes. Maybe you could add a
Communitylabel to this?Marking as duplicate of #9879
- removedDiscussionIssues which may not have code impactIssues which may not have code impact
on Nov 1, 2017 - addedDuplicateAn existing issue was already createdAn existing issue was already created
on Nov 1, 2017 typescript-bot commented
on Nov 16, 2017 ContributorMore actionsAutomatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
- locked and limited conversation to collaborators
on Jun 19, 2018
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?