Types 未定义流类型数组

Types 未定义流类型数组,types,flowtype,Types,Flowtype,使用.split()对数组项执行操作时遇到问题,因为FLOW认为它可能未定义 export const getTokenFromCookieRes = (cookies: string[]): mixed => { if (!cookies) { return undefined } if (0 in cookies) { return cookies[0] // cookies[0] returns an error for possibly being

使用.split()对数组项执行操作时遇到问题,因为FLOW认为它可能未定义

export const getTokenFromCookieRes = (cookies: string[]): mixed => {

  if (!cookies) {
    return undefined
  }

  if (0 in cookies) {
    return cookies[0] // cookies[0] returns an error for possibly being undefined
      .split(';')
      .find(c => c.trim().startsWith('jwt='))
      .split('=')[1]
  } else {
    return undefined
  }
}

问题不在于
cookies[0]
可能是
未定义的
;这是因为
find()
的结果可能是
undefined
。在尝试调用字符串上的
split()
之前,需要检查
find()
的结果

const getTokenFromCookieRes = (cookies?: string[]): mixed => {

  if (!cookies) {
    return undefined
  }

  if (!!cookies[0]) {
    const jwt = cookies[0] // No issues here
      .split(';')
      .find(c => c.trim().startsWith('jwt='))
      return jwt && jwt.split('=')[1];
  } 
}

问题不在于
cookies[0]
可能是
未定义的
;这是因为
find()
的结果可能是
undefined
。在尝试调用字符串上的
split()
之前,需要检查
find()
的结果

const getTokenFromCookieRes = (cookies?: string[]): mixed => {

  if (!cookies) {
    return undefined
  }

  if (!!cookies[0]) {
    const jwt = cookies[0] // No issues here
      .split(';')
      .find(c => c.trim().startsWith('jwt='))
      return jwt && jwt.split('=')[1];
  } 
}

谢谢-必须将它们放在单独的函数中才能返回值-但它成功了!谢谢-必须将它们放在单独的函数中才能返回值-但它成功了!