Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getUnknownType to checker api #60475

Open
6 tasks done
kirkwaiblinger opened this issue Nov 11, 2024 · 0 comments
Open
6 tasks done

Add getUnknownType to checker api #60475

kirkwaiblinger opened this issue Nov 11, 2024 · 0 comments

Comments

@kirkwaiblinger
Copy link

kirkwaiblinger commented Nov 11, 2024

🔍 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?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant