简体   繁体   English

使用tsc时出现“属性…在类型…上不存在”错误

[英]“Property … does not exist on type …” error when using tsc

I get the error when compiling a file with tsc: 使用tsc编译文件时出现错误:

Property 'qaz' does not exist on type '{ bar: string; }'.

With the following code in the file: 在文件中包含以下代码:

let foo = {
    bar: "Can you perform a Quirkafleeg?"
}

let { qaz = "I'm feeling manic!" } = foo;
console.log(qaz);
console.log(qaz.bar);

Based on example code on the page: https://zellwk.com/blog/es6/ in the section "Destructuring objects". 基于页面上的示例代码:“解构对象”部分中的https://zellwk.com/blog/es6/

I was expecting to get the second string as output, but I'm a bit confused as there is another example on that page along the lines of: 我原本希望将第二个字符串作为输出,但是我有点困惑,因为该页面上的另一个示例如下:

let { fizz: faz = "Eugene was my friend." } = foo; // as defined above
console.log(fizz);
console.log(fizz.faz);

Which gives a similar error: 这给出了类似的错误:

Property 'fizz' does not exist on type '{ bar: string; }'.

The article is about ES destructuring, which is also implemented in typescript. 这篇文章是关于ES解构的,它也可以在打字稿中实现。 Typescript will however perform extra checks. 但是,打字稿将执行额外的检查。 One of those checks is that you can't destructure from a type that does not declare the property. 这些检查之一是,您不能从未声明该属性的类型中解构。

So, this will be an error: 因此,这将是一个错误:

let foo = {
    bar: "Can you perform a Quirkafleeg?"
}

let { qaz = "I'm feeling manic!" } = foo; //err

While this will work: 尽管这将起作用:

let foo = {
    bar: "Can you perform a Quirkafleeg?",
    qaz: undefined
}

let { qaz = "I'm feeling manic!" } = foo; //ok qaz is "I'm feeling manic!" because of the default

Or you can specify the type explicitly: 或者,您可以显式指定类型:

let foo: { bar: string, qaz?: string} = {
    bar: "Can you perform a Quirkafleeg?",
}

let { qaz = "I'm feeling manic!" } = foo; //ok qaz is "I'm feeling manic!" because of the default

The other part is you can destructure a property to a variable of a different name, so below, we take bar from foo and put in fizz 另一部分是您可以将属性分解为其他名称的变量,因此在下面,我们从foobar并放入fizz

let foo = {
    bar: "Can you perform a Quirkafleeg?",
    qaz: undefined
}
let { fizz: bar = "Eugene was my friend." } = foo; // basically same as let fizz = foo.bar || "Eugene was my friend."
console.log(fizz); // "Can you perform a Quirkafleeg?" 

But the same typescript restriction applies, that bar in this case must be defined on foo . 但是,同样的打字稿限制适用,这bar在这种情况下,必须在定义foo

This is generally the extra layer of type checking typescript does, you can't access properties typescript does not know about, either through destructuring or direct access with . 这通常是类型检查typescript要做的额外一层,您无法访问typescript不知道的属性,无论是通过解构还是直接使用. or [] []

The last part, accessing console.log(fizz.faz); 最后一部分,访问console.log(fizz.faz); will never be valid, fizz is not the original object, it is the string that was in foo.bar , so it will not have a property faz or bar . 永远不会有效, fizz不是原始对象,它是foo.bar的字符串,因此它将没有属性fazbar I believe this is a misunderstand of the source blog on your part, I did not find any such claim in the blog. 我认为这是您对源博客的一种误解,我在博客中未发现任何此类主张。

That's not how Destructuring objects work. 那不是解构对象的工作方式。

Essentially it will look at the child properties of foo and assign new variable to them 本质上,它将查看foo的子属性并为其分配新变量

That must match the property names of foo 必须与foo的属性名称匹配

For instance 例如

const Zell = {
  firstName: 'Zell',
  lastName: 'Liew'
}

let { firstName, lastName } = Zell

console.log(firstName) // Zell
console.log(lastName) // Liew

to fix your example you'll need to do 要修正您的示例,您需要做

let foo = {
    bar: "Can you perform a Quirkafleeg?"
}

let { bar } = foo;
console.log(bar);

When you try to access 'qaz' on bar it cannot since bar is a String 当您尝试在bar上访问“ qaz”时,由于barString ,因此无法访问

EDIT: 编辑:

To expand in your example 在您的示例中进行扩展

let { qaz = "I'm feeling manic!" } = foo; When you're assigning qaz a value via = it is actually just a default value if it is not found on the object foo. 当您通过=分配qaz值时,如果在对象foo上找不到它,则实际上只是默认值。

In your example 在你的例子中

let { fizz: faz = "Eugene was my friend." } = foo; you're trying to assign fizz to the value of foo.faz and if fizz does not exist on foo it will default to "Eugene was my friend." 您正在尝试将fizz分配给foo.faz的值,并且如果foo不存在fizz ,它将默认为“ Eugene是我的朋友”。

That is working as expected. 这按预期工作。 You are destructing and providing a default parameter to qaz . 您正在破坏并向qaz提供默认参数。 It should work, but it seems like the tscompiler has an issue with that. 它应该可以工作,但是似乎tscompiler对此有问题。 You can fix it by providing the type any eg 您可以通过提供any类型的类型来修复它,例如

  let foo = { bar: "Can you perform a Quirkafleeg?" }
  let { qaz = "I'm feeling manic!" }: any = foo;
  console.log(qaz); // I'm feeling manic!
  console.log(qaz.bar); // undefined

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

相关问题 当属性存在时,类型错误时属性不存在 - Property does not exist on type error when the property does exist 使用自定义类型定义时,类型上不存在属性 - Property does not exist on type when using custom type definition 将匿名函数与D3 SVG符号一起使用时,“类型{}上不存在属性”错误 - “Property does not exist on type {}” error when using anonymous function with D3 SVG Symbol HTML 错误:“类型上不存在属性”即使未显示 - HTML Error: "Property does not exist on type" even when not displayed 从 JS 切换到 TS 时类型错误上不存在属性 - Property does not exist on type error when switching from JS to TS 使用本机Web组件时出现打字稿错误“类型'JSX.IntrinsicElements'上不存在属性” - Typescript error “Property does not exist on type 'JSX.IntrinsicElements'” when using native web component 使用新代理({})时,类型“{}”上不存在属性“foo”,.....) - Property 'foo' does not exist on type '{}' when using new Proxy({}), .....) 错误:类型 HTMLElement 上不存在属性“选择” - error:Property 'select' does not exist on type HTMLElement 属性在类型字符串错误上不存在 - Property does not exist on type String error “LoginComponent”类型上不存在属性“错误” - Property 'error' does not exist on type 'LoginComponent'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM