导入流式不相交联合?
Import flowtype disjoint union?
在当前的 react/redux 项目上使用 flowtype。
我在 actions.js 文件中定义了一个不相交的联合类型:
export type ArticleAction =
{ type: 'ARTICLE_SET_EDITION' }
| { type: 'ARTICLE_BLABLA', blip: string };
然后在我的减速器中我有
import type { ArticleAction } from './actions';
[...]
const articlesReducer = (state: any = initialState, action: ArticleAction): any => {
if (action.type === 'ARTICLE_BLABLA') {
const test = action.blip.shoups;
return test;
}
}
流程未检测到问题。
但是!如果我直接在 reducer.js 中声明 ArticleAction,它会识别出 action.blip.shoups 是无效的,因为 blip 是一个字符串。
知道我做错了什么吗?
谢谢
TL;DR Flow doesn't error in situations like this today, but most likely will in the future.
这与 import/exports 甚至联合类型都没有任何关系,您可以将其一直简化为:
function method(val: 'foo') {
if (val === 'bar') {
// unreachable...
}
}
Flow可以看出是不可能的细化,可以知道内码是不可达的。但是Flow在不可达的场景下不会报错。今天,它只是将 val
的值标记为该代码路径中的 "empty" 类型,然后继续。
我们已经开始为此 可达性 分析奠定基础,并将在 Flow 的未来版本中使用它来创建错误。
我们还可以使用可达性分析来测试穷尽性,即:
function method(val: 'foo' | 'bar') {
if (val === 'foo') {
// ...
} else if (val === 'bar') {
// ...
} else {
// possibilities of val have been exhausted, this is unreachable...
}
}
这些是常见的请求,我们正在处理它们。
在当前的 react/redux 项目上使用 flowtype。
我在 actions.js 文件中定义了一个不相交的联合类型:
export type ArticleAction =
{ type: 'ARTICLE_SET_EDITION' }
| { type: 'ARTICLE_BLABLA', blip: string };
然后在我的减速器中我有
import type { ArticleAction } from './actions';
[...]
const articlesReducer = (state: any = initialState, action: ArticleAction): any => {
if (action.type === 'ARTICLE_BLABLA') {
const test = action.blip.shoups;
return test;
}
}
流程未检测到问题。
但是!如果我直接在 reducer.js 中声明 ArticleAction,它会识别出 action.blip.shoups 是无效的,因为 blip 是一个字符串。
知道我做错了什么吗? 谢谢
TL;DR Flow doesn't error in situations like this today, but most likely will in the future.
这与 import/exports 甚至联合类型都没有任何关系,您可以将其一直简化为:
function method(val: 'foo') {
if (val === 'bar') {
// unreachable...
}
}
Flow可以看出是不可能的细化,可以知道内码是不可达的。但是Flow在不可达的场景下不会报错。今天,它只是将 val
的值标记为该代码路径中的 "empty" 类型,然后继续。
我们已经开始为此 可达性 分析奠定基础,并将在 Flow 的未来版本中使用它来创建错误。
我们还可以使用可达性分析来测试穷尽性,即:
function method(val: 'foo' | 'bar') {
if (val === 'foo') {
// ...
} else if (val === 'bar') {
// ...
} else {
// possibilities of val have been exhausted, this is unreachable...
}
}
这些是常见的请求,我们正在处理它们。