IssueRaiser

Proposal: Type Relationship API

They type checker has no public API for checking type relationships or for looking up types in a given scope. This can make writing semantic-analysis informed lint rules very difficult (either because you need to replicate a lot of logic, or because you simply don't have enough information). Since [tslint enabled semantic checks](https://github.com/palantir/tslint/commit/9248e056c1a652ee13597e68da26cacf0b87e134) we now have a popular consumer who would very much like to be able to perform meaningful semantic comparisons (See [this rule](https://github.com/palantir/tslint/pull/1394#issuecomment-234384650) for instance). # Proposal A small new set of public API methods on the type checker: ``` ts interface TypeChecker { isIdenticalTo(a: Type, b: Type): boolean; // Exposes internal identity relationship isSubtypeOf(a: Type, b: Type): boolean; // Exposes internal structural subtype relationship isAssignableTo(a: Type, b: Type): boolean; // Exposes internal assignable relationship isComparableTo(a: Type, b: Type): boolean; // Exposes internal comparable relationship isInstantiationOf(a: GenericType, b: GenericType): boolean; // While not an internal relationship, it is a straightforward semantic question to ask an answer which requires internal APIs to answer lookupGlobalType(name: string): Type; // Looks up a global symbol named "name" with meaning SymbolFlags.Type (Identical to getGlobalType but without the expected arity based return value) - returns unknownType

Posted by weswigham on Jul 21, 2016, 10:39 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.5,000 issues0 comments

ux (Software)

Proposal: Type Relationship API

weswighamJul 21, 2016, 10:39 PM

They type checker has no public API for checking type relationships or for looking up types in a given scope. This can make writing semantic-analysis informed lint rules very difficult (either because you need to replicate a lot of logic, or because you simply don't have enough information). Since [tslint enabled semantic checks](https://github.com/palantir/tslint/commit/9248e056c1a652ee13597e68da26cacf0b87e134) we now have a popular consumer who would very much like to be able to perform meaningful semantic comparisons (See [this rule](https://github.com/palantir/tslint/pull/1394#issuecomment-234384650) for instance). # Proposal A small new set of public API methods on the type checker: ``` ts interface TypeChecker { isIdenticalTo(a: Type, b: Type): boolean; // Exposes internal identity relationship isSubtypeOf(a: Type, b: Type): boolean; // Exposes internal structural subtype relationship isAssignableTo(a: Type, b: Type): boolean; // Exposes internal assignable relationship isComparableTo(a: Type, b: Type): boolean; // Exposes internal comparable relationship isInstantiationOf(a: GenericType, b: GenericType): boolean; // While not an internal relationship, it is a straightforward semantic question to ask an answer which requires internal APIs to answer lookupGlobalType(name: string): Type; // Looks up a global symbol named "name" with meaning SymbolFlags.Type (Identical to getGlobalType but without the expected arity based return value) - returns unknownType

Comments (0)

No comments yet. Start the discussion.

Comment on this issue