简体   繁体   English

在另一个函数中使用时,变量可能未定义(打字稿“错误TS2532”)

[英]Variable possibly undefined when used inside another function (Typescript “error TS2532)

So here we go, trying Typescript. 因此,我们开始尝试Typescript。 I got this function for view for my automatic global component registration in Vue. 我在Vue中自动全局组件注册时可以查看此功能。 To be honest I have no idea on how this would translate in Typescript. 老实说,我不知道如何在Typescript中进行翻译。 It's the fileName inside camelCase(fileName.split('/')) function. 它的fileNamecamelCase(fileName.split('/'))功能。

import upperFirst from 'lodash/upperFirst'
import camelCase from 'lodash/camelCase'


// other code

requireComponent.keys().forEach(fileName => {
  const componentConfig = requireComponent(fileName)

  // here
  const componentName = upperFirst(
    camelCase(
      fileName
        .split('/')
        .pop()
        .replace(/\.\w+$/, '')
    )
  )

  Vue.component(
    componentName,
    componentConfig.default || componentConfig
  )
})

Typescript error TS2532: Object is possibly 'undefined' 打字稿错误TS2532:对象可能是“未定义”

pop returns string | undefined pop返回string | undefined string | undefined , so you can either save that in a variable to check for undefined or be bold and claim that it will always be defined using ! string | undefined ,因此您可以将其保存在变量中以检查undefined也可以将其加粗并声称将始终使用!进行定义! :

  fileName
    .split('/')
    .pop()!
    .replace(/\.\w+$/, '')

(It is not really that bold because splitting an empty string will always return at least an array containing the empty string, so pop should resolve to an element.) (实际上并不是那么粗体,因为分割空字符串将始终至少返回一个包含空字符串的数组,因此pop应该解析为一个元素。)


Variable check would look something like this: 变量检查看起来像这样:

const last = fileName
    .split('/')
    .pop();
if (last == undefined)
    throw Error("This has never happened before.")

// Compiler now will 'know' that last is a string.
camelCase(last.replace(/\.\w+$/, ''));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿数组语义错误 TS2532:对象可能是“未定义” - Typescript array semantic error TS2532: Object is possibly 'undefined' TypeScript 错误 TS2532:Object 可能是“未定义”? - TypeScript Error TS2532: Object is possibly 'undefined'? Typescript 显示“this”错误 Object 说 TS2532: Object 在 vue 方法中可能是“未定义” - Typescript displays error for "this" Object saying TS2532: Object is possibly 'undefined' inside of vue methods TypeScript 2:Import语句生成TS2532:“对象可能是'undefined'。” - TypeScript 2: Import statement generates TS2532: “Object is possibly 'undefined'.” 类型保护错误TS2532后,可能未定义Typescript对象 - Typescript Object is possibly undefined after type guard error TS2532 错误 TS2532:对象可能是“未定义” - 使用“?” 操作员 - error TS2532: Object is possibly 'undefined' - Using "?." operator 对象可能是“未定义” ts2532 - object is possibly "undefined" ts2532 Ts2532、Object 可能是“未定义” - Ts2532, Object is possibly 'undefined' TS2532:对象可能是“未定义”。 在 array.map() 上 - TS2532: Object is possibly 'undefined'. on array.map() 打字稿-内联未定义检查不起作用(对象可能是'undefined'.ts(2532)) - Typescript - Inline undefined check not working (Object is possibly 'undefined'.ts(2532))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM