microsoft / microsoft/TypeScript

Check JS doesn't narrow types properly

Open
#39,132 2 comments 0 reactions 0 assignees View on GitHub

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:

https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=51&pc=1&useJavaScript=true#code/JYWwDg9gTgLgBAbzgVwM4FMBK6AmyDG6UcAvnAGZQQhwDkU6AhvjLQNwBQHA9AFS8c4vOAAEY6cABtG4xEhgBPMOgBccVDCjAAdgHM2cMIwWSIjHAH41jbQoMkyAQRbAI2wcLESw02QggARgBW6CxkAMowMugeouJS0XKGjBpqkdEA2gC6BmAMGNowaVHiBuTIMMgMxZk5pGQAqto4EOnisV4JfvJKqnQNAHIAIgDy7KRwAD6IcIrKarSYAKKj4w5wTS3OMK7uQqJGUIw0CEgAFBrRNeIANHDMO25q27sAlNfo9mQMeIRQHYdjog2ugyDpgDtGJIQR5uBx0AAPSCwCjIbQuNwoDCbCDYX5EM4-AhEO7gyHQkroV6IQRwPgCOCMzyAk44kFkS7tRn7EQsxA4l6Y6aC7RkB67WlCOGM-BuDQoZq43DE4gAXjgF0pd3FbmpqoAfDTueoAO4Q-AACw1Ou0ADo5lSjca4PgUuh+sMxmoEJLnbLtPKMtp0CaAAr5dCFO62mPBsMpGBZODqznoW1GDScX3GhiVKDaJ3O7kZopwOOhhM3bPOvLoAql8sRqPV43lPN9DKp9NNmDRmNdttVdBZKtFxkkThFkjV10YOjLVbelsuuXwIMh8N1yO9uAx21xgBiFSHSZTlNtg4YWbHcFzVQLPpvjJLak759r9b7tq7JZHy+fPZqI2W7Nk+qLtkBIZHu2o5jhO1bTkWODoOQjDIJIRTLne+aFmOL5wG+0TdiBO57j+CZ-mBH7bmoRJ-JqRHUVG9wYtorywTel4dpRcGTtyiGkNet7oHmBZoFgyr0WiLR4iqdyPsWCavjxAEkWoZLAFCIIceBQ7KbBJCvJw05AA

Related Issues:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.