microsoft / microsoft/TypeScript
Type Inference Enhancement of Handler Args
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
🔍 Search Terms
"type inference", "type narrowing"
✅ 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
Introduction
When I write React codes with TypeScript, I sometimes define Props as the following.
type Props = ({
parentIds: string[];
onChange: (event: { id: string }) => void
onChange2: () => void
} | {
parentIds?: never;
onChange: (event: { id: number }) => void;
})
It says, "When parentId is passed, string id is passed to onChange handler and you can use onChange2. If it is not passed, number is is passed to onChange handler".
But it doesn't work correctly. When I use it, as the following
function Component(props: Props) {
return null
}
function App() {
return (
<Component
parentIds={[]}
// Expected: Parameter e is { id: string }
// Actual: Parameter 'e' implicitly has an 'any' type.(7006)
onChange={e => {
if (e.id) {
// Do nothing
} else {
// Do nothing
}
}}
onChange2={() => {
console.log("parentId is detected as string[]")
}}
/>
)
}
The parameter e of onChange is inferred as any, not { id: string }.
I can pass onChange2 handler so it seems type narrowing works, but type inference of the args of handler doesn't work.
Suggestion
How about enhancing type inference in such case?
📃 Motivating Example
When you want to use same function or component for various purposes.
💻 Use Cases
- What do you want to use this for?
- What shortcomings exist with current approaches?
- What workarounds are you using in the meantime?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the linked TypeScript Playground example and confirm the implicit-any diagnostic (7006) for the handler parameter. No source file or test is named, so first locate the type-inference and contextual-typing entry points, then establish tests showing that the parameter is inferred as { id: string } while preserving the existing narrowing behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100