密封盒 类 流
sealed case classes in flow
我正在尝试模仿 Scala 的 sealed case classes in Flow by using disjoint unions:
type ADD_TODO = {
type:'ADD_TODO',
text:string,
id:number
}
type TOGGLE_TODO = {type:'TOGGLE_TODO', id:number }
type TodoActionTy = ADD_TODO | TOGGLE_TODO
const todo = (todo:TodoTy, action:TodoActionTy) => {
switch (action.type){
case 'ADD_TODO' :
return { id:action.id, text:action.text, completed: false};
case 'TOGGGGLE_TODO': // this should give a type error
if (todo.id !== action.id) {return todo;}
return {...todo, completed:!todo.completed};
}
}
我应该得到 case 'TOGGGGLE_TODO':
的类型错误,但我没有。
有办法解决这个问题吗?
编辑:
我将 Gabriele 的评论中的代码粘贴到此处以确保未来的可靠性:
type TodoTy = {};
type ADD_TODO = { type: 'ADD_TODO', text: string, id: number };
type TOGGLE_TODO = { type: 'TOGGLE_TODO', id: number };
type TodoActionTy = ADD_TODO | TOGGLE_TODO;
export const todo = (todo: TodoTy, action: TodoActionTy) => {
switch (action.type){
case 'ADD_TODO': break;
// Uncomment this line to make the match exaustive and make flow typecheck
//case 'TOGGLE_TODO': break;
default: (action: empty)
}
}
empty
类型可以用来验证 Flow 是穷举性的
export const todo = (todo: TodoTy, action: TodoActionTy) => {
switch (action.type){
case 'ADD_TODO' :
...
case 'TOGGGGLE_TODO':
...
default :
// only true if we handled all cases
(action: empty)
// (optional) handle return type
throw 'unknown action'
}
}
我正在尝试模仿 Scala 的 sealed case classes in Flow by using disjoint unions:
type ADD_TODO = {
type:'ADD_TODO',
text:string,
id:number
}
type TOGGLE_TODO = {type:'TOGGLE_TODO', id:number }
type TodoActionTy = ADD_TODO | TOGGLE_TODO
const todo = (todo:TodoTy, action:TodoActionTy) => {
switch (action.type){
case 'ADD_TODO' :
return { id:action.id, text:action.text, completed: false};
case 'TOGGGGLE_TODO': // this should give a type error
if (todo.id !== action.id) {return todo;}
return {...todo, completed:!todo.completed};
}
}
我应该得到 case 'TOGGGGLE_TODO':
的类型错误,但我没有。
有办法解决这个问题吗?
编辑:
我将 Gabriele 的评论中的代码粘贴到此处以确保未来的可靠性:
type TodoTy = {};
type ADD_TODO = { type: 'ADD_TODO', text: string, id: number };
type TOGGLE_TODO = { type: 'TOGGLE_TODO', id: number };
type TodoActionTy = ADD_TODO | TOGGLE_TODO;
export const todo = (todo: TodoTy, action: TodoActionTy) => {
switch (action.type){
case 'ADD_TODO': break;
// Uncomment this line to make the match exaustive and make flow typecheck
//case 'TOGGLE_TODO': break;
default: (action: empty)
}
}
empty
类型可以用来验证 Flow 是穷举性的
export const todo = (todo: TodoTy, action: TodoActionTy) => {
switch (action.type){
case 'ADD_TODO' :
...
case 'TOGGGGLE_TODO':
...
default :
// only true if we handled all cases
(action: empty)
// (optional) handle return type
throw 'unknown action'
}
}