简体   繁体   English

在 typescript class 属性中添加可选链接后更改了变量名称

[英]Variable name is changed after adding optional chaining in typescript class property

Hi I have written one typescript file.嗨,我已经写了一个 typescript 文件。 What's happening when I am trying to invoke some function(ie this.details.name.fullname()) and if fullname() function is not available then I get TypeError: this.details.name.fullname is not a function in catch block.当我尝试调用某些函数(即 this.details.name.fullname())并且如果 fullname() function 不可用时发生了什么,那么我得到TypeError: this.details.name.fullname is not a function in catch 块. Which gives the complete information.其中提供了完整的信息。 But when I add optional chaining (ie this.details.name?.fullname()) then I get error TypeError: _a.fullname is not a function .但是当我添加可选链接(即 this.details.name?.fullname())时,我得到错误TypeError: _a.fullname is not a function _a is not giving any information what is object or anything. _a 没有提供任何信息,什么是 object 或任何东西。

The code I have tried is below我尝试过的代码如下

dummy.ts -: dummy.ts -:

type details = {
    name: {
        firstname: string;
        lastname?: any;
        fullname?: any
    }
}
class Test {
    private details: details;
    constructor(details: details) {
        this.details = details;
    }

    public async printName() {
        try {
            this.details.name?.fullname()
        } catch (err) {
            console.log(err)
        }
    }
}


const obj = new Test({ name: { firstname: 'ashish', lastname: 'butola' } })

async function xyz() {
    await obj.printName()
}

xyz()

tsconfig.json -: tsconfig.json -:

  {
    "compilerOptions": {
      "module": "commonjs",
      "target": "es2015",
      "rootDir": "./src",
      "outDir": "./build",
      "inlineSourceMap": false,
      "strict": true,
      "noImplicitAny": false,
      "noImplicitThis": false,
      "strictNullChecks": true,
      "noUnusedLocals": true,
      "noUnusedParameters": true,
      "experimentalDecorators": true,
      "lib": [
        "dom",
        "es2017"
      ]
    },
    "include": [
      "src"
    ],
    "exclude": [
      "node_modules",
      "build",
      ".vscode"
    ]
  }
  

I want the exact message ie(TypeError: this.details.name.fullname is not a function) but at the same time I want to add optional chaining so it doesn't break because name can be undefined sometime.我想要确切的消息,即(TypeError:this.details.name.fullname 不是函数),但同时我想添加可选的链接,这样它就不会中断,因为名称有时可能是未定义的。 I think I am missing some property in tsconfig.json file.我想我在 tsconfig.json 文件中缺少一些属性。

Your current code optionally gets the name property of the details object, and then calls the fullname function on that value, unless the entire details object is undefined.您当前的代码可以选择获取详细信息 object 的name属性,然后在该值上调用fullname名 function,除非整个详细信息 object 未定义。 You need to optionally call the function too, to account for the case where the object exists, but the function does not:您还需要选择调用 function,以解决 object 存在的情况,但 function 不存在:

this.details.name?.fullname?.()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM