简体   繁体   English

TS2339:类型“ {}”上不存在属性“ props”

[英]TS2339: Property 'props' does not exist on type '{}'

I'm trying access a property of an object which can be {} . 我正在尝试访问可以是{}的对象的属性。

So I'm doing something like this: 所以我正在做这样的事情:

// component is the object which can be emtpy {}
if (component.children) return method(component.children)

Although, even if I'm ensuring that .children is there, typescript complains that it does not exist (also in the if condition). 尽管即使我确保.children在其中,打字稿仍会抱怨它不存在(也处于if条件下)。

This is quite weird IMO, because if we're asserting that a property exists (or not) why would it complain? 这是非常奇怪的IMO,因为如果我们断言某个属性存在(或不存在),为什么会抱怨呢?


Example: 例:

type OtherType = { name: string };
type TestType = {} | OtherType;

function method(variable: TestType): string {
  if (!variable.name) return ''

  return variable.name;
}

This will throw Property 'name' does not exist on type '{}'. 这将引发Property 'name' does not exist on type '{}'. as it can be seen in TypeScript Playground TypeScript Playground中可以看到

The error makes sense — it's not safe to access a property that may not exist. 该错误是有道理的-访问可能不存在的属性并不安全。 Replace it with: 替换为:

function method(variable: TestType): string {
  if ('name' in variable) {
      return variable.name
  }

  return '';
}

As to the first part of your question, It's hard to answer without knowing what method does. 至于问题的第一部分,不知道哪种method很难回答。

I came across this while coding with StencilJS. 我在用StencilJS编码时遇到了这个问题。 When you create a Web Component with StencilJS, you also define a "tag" attribute for it. 使用StencilJS创建Web组件时,还为其定义了一个“标签”属性。 Then you refer to it using that tag everywhere else in your project. 然后,您可以在项目的其他任何地方使用该标记来引用它。 For eg if your Web Component is CustomDropdown, you will have 例如,如果您的Web组件是CustomDropdown,您将拥有

    @Component({
    tag: 'custom-dropdown',
    styleUrl: 'customdropdown.scss',
    shadow: false
})

If you need to use this component in another component, you simply use the "tag": 如果需要在另一个组件中使用此组件,则只需使用“标签”:

    <custom-dropdown> etc... </custom-dropdown>

If you use 如果您使用

    <CustomDropdown> 

you will get the above error on the attributes. 您将在属性上收到上述错误。

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

相关问题 如何在Typescript中为其子项设置功能道具类型? TS2339类型上不存在“孩子”属性 - How to set function props type for its child in Typescript? Property 'children' does not exist on type TS2339 错误TS2339:类型“字符串”上不存在属性“默认”。** - Error TS2339: Property 'Default' does not exist on type 'string'.** TS2339:“元素”类型上不存在属性“样式” - TS2339: Property 'style' does not exist on type 'Element' 输入字段上的“TS2339:属性...在类型...上不存在” - “TS2339: Property … does not exist on type …”, on input field TS2339:“字符串”类型上不存在属性“包含” - TS2339: Property 'includes' does not exist on type 'string' 错误TS2339:类型“ HTMLElement”上不存在属性“ imageID” - error TS2339: Property 'imageID' does not exist on type 'HTMLElement' 类型“从不”上不存在属性“单击”。 TS2339 - Property 'click' does not exist on type 'never'. TS2339 TS2339 属性 myProperty 在 SetStateAction 类型上不存在<user></user> - TS2339 Property myProperty does not exist on type SetStateAction<User> 类型“JSX.IntrinsicElements”上不存在属性。 TS2339 - Property does not exist on type 'JSX.IntrinsicElements'. TS2339 错误TS2339:类型“ {}”上不存在属性“ forEach” - error TS2339: Property 'forEach' does not exist on type '{}'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM