string.match() 的流类型问题
Flow type issue with string.match()
我的程序中有一个脚本最终会调用一个接受字符串并在该字符串上运行 .match(regex)
的函数。
根据 MDN:
String.prototype.match(regex)
returns 一种 array
数据类型,我只需要访问第一个索引 [0]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match
我尝试以各种方式重新组织此脚本,但出现流程错误:
"Cannot get findStationId(...)[0]
because an index signature declaring the expected key / value type is missing in null [1]."
函数的组织不是我遇到问题的地方,而是 [0]
索引引用。
我怎样才能正确输入检查并声明预期值?
// @flow
type RssResults = Array<{
title: string,
contentSnippet: string
}>
const findStationId = (string: string): Array<any> | null => string.match(/([0-9]{5}|[A-Z\d]{5})/)
export default (rssResults: RssResults) => {
const entries = []
rssResults.forEach((entry) => {
const observation = {}
observation.title = entry.title
const id = findStationId(entry.title)[0] // flow errors here on [0]
observation.id = id.toLowerCase()
// ...
entries.push(observation)
})
return entries
}
.flowconfig
[ignore]
.*/test/*
[include]
[libs]
[lints]
[options]
module.file_ext=.js
module.file_ext=.jsx
module.file_ext=.json
module.file_ext=.css
module.file_ext=.scss
module.name_mapper.extension='css' -> 'empty/object'
module.name_mapper.extension='scss' -> 'empty/object'
[strict]
[untyped]
.*/node_modules/**/.*
谢谢!
如果您仔细查看该 MDN 页面,它指出:
An Array whose contents depend on the presence or absence of the global (g) flag, or null if no matches are found.
其中 "null if no matches are found." 是这里的关键部分。 null[0]
没有意义。你自己的return类型Array<any> | null
也提到了这个。
所以要么你的
const id = findStationId(entry.title)[0]
应该做
const match = findStationId(entry.title)
if (!match) throw new Error("No station ID found")
const id = match[0]
或者您应该将 findStationId
更改为不允许 null
return.
const findStationId = (string: string): Array<any> => {
const match = string.match(/([0-9]{5}|[A-Z\d]{5})/)
if (!match) throw new Error("No station ID found")
return match
}
我的程序中有一个脚本最终会调用一个接受字符串并在该字符串上运行 .match(regex)
的函数。
根据 MDN:
String.prototype.match(regex)
returns 一种 array
数据类型,我只需要访问第一个索引 [0]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match
我尝试以各种方式重新组织此脚本,但出现流程错误:
"Cannot get
findStationId(...)[0]
because an index signature declaring the expected key / value type is missing in null [1]."
函数的组织不是我遇到问题的地方,而是 [0]
索引引用。
我怎样才能正确输入检查并声明预期值?
// @flow
type RssResults = Array<{
title: string,
contentSnippet: string
}>
const findStationId = (string: string): Array<any> | null => string.match(/([0-9]{5}|[A-Z\d]{5})/)
export default (rssResults: RssResults) => {
const entries = []
rssResults.forEach((entry) => {
const observation = {}
observation.title = entry.title
const id = findStationId(entry.title)[0] // flow errors here on [0]
observation.id = id.toLowerCase()
// ...
entries.push(observation)
})
return entries
}
.flowconfig
[ignore]
.*/test/*
[include]
[libs]
[lints]
[options]
module.file_ext=.js
module.file_ext=.jsx
module.file_ext=.json
module.file_ext=.css
module.file_ext=.scss
module.name_mapper.extension='css' -> 'empty/object'
module.name_mapper.extension='scss' -> 'empty/object'
[strict]
[untyped]
.*/node_modules/**/.*
谢谢!
如果您仔细查看该 MDN 页面,它指出:
An Array whose contents depend on the presence or absence of the global (g) flag, or null if no matches are found.
其中 "null if no matches are found." 是这里的关键部分。 null[0]
没有意义。你自己的return类型Array<any> | null
也提到了这个。
所以要么你的
const id = findStationId(entry.title)[0]
应该做
const match = findStationId(entry.title)
if (!match) throw new Error("No station ID found")
const id = match[0]
或者您应该将 findStationId
更改为不允许 null
return.
const findStationId = (string: string): Array<any> => {
const match = string.match(/([0-9]{5}|[A-Z\d]{5})/)
if (!match) throw new Error("No station ID found")
return match
}