为什么 flow 让我检查一个已经定义的未定义值?
Why does flow make me check for an undefined value that is already defined?
如果我不检查 response
是否存在,下面的代码流会抛出一个错误。但是 const response
定义应该保证响应可用。为什么流不接受省略 response
是否存在的检查?
/* @flow */
// ... import dependencies
export function* loadDepartments(): Generator<*, *, *> {
try {
const response = yield call(getJson, endpoints.departments);
if (response && typeof response.data !== 'undefined') {
yield put(actions.loadingDepartmentsSucceeded(response.data));
}
} catch (errors) {
yield put(actions.loadingDepartmentsFailed(errors));
}
}
如果您使用 const
事件,response
的值可以是 undefined
,具体取决于您的 call
函数产生的结果。用 const
定义的变量确实可以包含值 undefined
.
如果response
未定义,则检查
if (typeof response.data !== 'undefined') {
会引发类型错误,因为您无法读取 undefined
.
的 属性 "data"
如果我不检查 response
是否存在,下面的代码流会抛出一个错误。但是 const response
定义应该保证响应可用。为什么流不接受省略 response
是否存在的检查?
/* @flow */
// ... import dependencies
export function* loadDepartments(): Generator<*, *, *> {
try {
const response = yield call(getJson, endpoints.departments);
if (response && typeof response.data !== 'undefined') {
yield put(actions.loadingDepartmentsSucceeded(response.data));
}
} catch (errors) {
yield put(actions.loadingDepartmentsFailed(errors));
}
}
如果您使用 const
事件,response
的值可以是 undefined
,具体取决于您的 call
函数产生的结果。用 const
定义的变量确实可以包含值 undefined
.
如果response
未定义,则检查
if (typeof response.data !== 'undefined') {
会引发类型错误,因为您无法读取 undefined
.