microsoft / microsoft/TypeScript
Check JS doesn't narrow types properly
Open
Nobody has claimed this yet.
Needs Investigation
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 3.7.x-dev.201xxxxx
Search Terms:
Code
import { useReducer } from 'react';
/**
* @template {{ type: string; payload?: any; }} Action
* @template {object} State
* @template {{ past: State[]; present: State; future: State[]; }} UndoState
* @template {{ type: 'UNDO'; } | { type: 'REDO'; }} UndoAction
* @param {{ (state: State, action: Action): State; }} reducer
* @param {State} initialState
*/
export function useUndoReducer(reducer, initialState) {
/**
* @param {UndoState} state
* @param {UndoAction | Action} action
*/
const undoReducer = (state, action) => {
switch (action.type) {
case 'UNDO': {
const [newPresent, ...newPast] = state.past;
return {
past: newPast,
present: newPresent,
future: [state.present, ...state.future],
};
}
case 'REDO': {
const [newPresent, ...newFuture] = state.future;
return {
past: [state.present, ...state.past],
present: newPresent,
future: newFuture,
};
}
default:
return {
past: [state.present, ...state.past],
// @ts-ignore
present: reducer(state.present, action),
future: [],
};
}
};
return useReducer(undoReducer, {
past: [],
present: initialState,
future: [],
});
}
Expected behavior:
not having to use a @ts-ignore
Actual behavior:
had to use @ts-ignore
Playground Link:
Related Issues:
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
Start with the linked TypeScript Playground reproduction using the reported 3.7.x-dev version and inspect how the JSDoc types are narrowed through the switch on action.type. Compare the reported reducer call with the expected types; done means the example no longer requires @ts-ignore while retaining the stated reducer and action types.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react, typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100