-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
CommittedThe team has roadmapped this issueThe team has roadmapped this issueHelp WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
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?
RyanCavanaugh, a-tarasyuk, ronami, bradzacher and controversial
Metadata
Metadata
Assignees
Labels
CommittedThe team has roadmapped this issueThe team has roadmapped this issueHelp WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript