Skip to content

Add getUnknownType to checker api #60475

Closed
@kirkwaiblinger

Description

@kirkwaiblinger

🔍 Search Terms

unknown, ts api, type relationship api,

✅ Viability Checklist

⭐ Suggestion

I would like to add a method to the checker api to get an intrinsic unknown type.

    const checker: TypeChecker = {
         // ...
         getAnyType: () => anyType,
+        getUnknownType: () => unknownType,
         getStringType: () => stringType,
        // ...
    }

📃 Motivating Example

I want to write a function that looks roughly like

import * as ts from 'typescript';
import * as tsutils from 'ts-api-utils';

export function getConstrainedType(checker: ts.TypeChecker, type: ts.Type): ts.Type {
  return !tsutils.isTypeParameter(type)
    ? type
    : checker.getBaseConstraintOfType(type) ?? checker.getUnknownType();
}

for use in typescript-eslint (see the utility function getConstrainedTypeAtLocation, which currently cannot be used to resolve typescript-eslint/typescript-eslint#10314 (comment), due to an unconstrained generic not returning an unknown type with that utility's current implementation)

I'm happy to submit a PR to make this change.

💻 Use Cases

  1. What do you want to use this for?
  2. What shortcomings exist with current approaches?
    • cannot write linter code that handles the types irrespective of whether it is a generic or ordinary type, due to no way to map an unconstrained generic to an intrinsic unknown type object
  3. What workarounds are you using in the meantime?

Metadata

Metadata

Assignees

No one assigned

    Labels

    CommittedThe team has roadmapped this issueHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions