Closed
Description
🔍 Search Terms
unknown, ts api, type relationship api,
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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
- What do you want to use this for?
- 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
- 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
- What workarounds are you using in the meantime?