Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可以使用 as const 改变类型推导方式 #10

Open
wind8866 opened this issue Jan 10, 2022 · 0 comments
Open

可以使用 as const 改变类型推导方式 #10

wind8866 opened this issue Jan 10, 2022 · 0 comments

Comments

@wind8866
Copy link

注意📌列,推导出的 type 不是 ActionTodoConstants,而是精确类型 ActionTodoConstants.ADD_TODO,没有必要手动写类型声明,只需纠正默认推导的方式即可

// constants/todo.ts
export enum ActionTodoConstants {
  ADD_TODO = 'todo/add',
  TOGGLE_TODO = 'todo/toggle',
}

// actions/todo.ts
let id = 0
const addTodo = (name: string) => ({
  payload: {
    todo: {
      done: false,
      id: id++,
      name,
    },
  },
  type: ActionTodoConstants.ADD_TODO as const,// 📌
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant