question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: Type Relationship API

See original GitHub issue

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 we now have a popular consumer who would very much like to be able to perform meaningful semantic comparisons (See this rule for instance).

Proposal

A small new set of public API methods on the type checker:

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 on failure
  lookupTypeAt(name: string, position: Node): Type // Resolve a symbol lexically at the position specified with meaning SymbolFlags.Type (identical to resolveName, but with less levers) - returns unknownType on failure

  getAnyType(): IntrinsicType;
  getStringType(): IntrinsicType;
  getNumberType(): IntrinsicType;
  getBooleanType(): IntrinsicType;
  getVoidType(): IntrinsicType;
  getUndefinedType(): IntrinsicType;
  getNullType(): IntrinsicType;
  getESSymbolType(): IntrinsicType;
  getNeverType(): IntrinsicType;
  getUnknownType(): IntrinsicType;
  getStringLiteralType(text: string): StringLiteralType; // When numeric literal types merge, they should have a similar endpoint
}

While this isn’t a complete set of APIs for manipulating the type checker (I’ve left off an API to programatically create new types for comparison), it is enough to do meaningful analysis with existing types in the compilation without resorting to brittle (or often wrong) syntax-based solutions.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:110
  • Comments:31 (16 by maintainers)

github_iconTop GitHub Comments

12reactions
ricticcommented, Feb 12, 2019

I’ve just discovered https://www.npmjs.com/package/ts-simple-type by way of https://twitter.com/RuneMehlsen/status/1095401856493330433.

It looks like a reasonable alternative until the official TS api is public.

11reactions
ghostcommented, Jul 3, 2017

Is there any progress on this? It would be incredibly useful to have. Just an isTypeAssignableTo would be great. Right now the only option is to link to a customly patched version of TypeScript.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Filtering/querying (deep) relationships - a proposal for a syntax
Consider the following API: GET /authors to get all resources of type author; Resource author has relationship articles which is to- ...
Read more >
REST API for CX Sales and B2B Service
The relationships resource is used to view, create, update, and delete a relationship. It is an object that includes attributes used to store...
Read more >
API Improvement Proposals: Google's Take on the API Style ...
Google's API Improvement Proposals are a unique take on the style guide, leveraging GitHub to enable collaborativeness and iterative ...
Read more >
OpportunityHistory | Object Reference for the Salesforce ...
Available in API version 50.0 and later. PrevCloseDate. Type: date; Properties: Filter, Group, Nillable, Sort; Description: The value in the opportunity's ...
Read more >
Fact sheet Advancing Interoperability and Improving Prior ...
The proposals would place new requirements on Medicare Advantage (MA) ... via the already-established Patient Access API, we propose to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found