简体   繁体   English

typescript 错误:2339。 如何在一个变量中使用多种类型的 object?

[英]typescript error:2339. how can I use multiple types of object in one variable?

I'm trying to make component with vanilla js + typescript我正在尝试使用 vanilla js + typescript 制作组件

I made class "Component".我制作了 class “组件”。 the problem is "state"问题是“状态”

interface boardState {
  items: string[];
  page: number;
}
interface postState {
  writer: string;
  content: string;
}
class Component {
  $target : Element;
  state: boardState | postState;
  constructor() { ...}
  init() { ...}
  setState() { ...}
  render() { ...}
}
class Board extends Component{
  constructor(selector) {
    super(selector);
  }
  init(){
    this.state.items = ["title1", "title2"] ; // error
  }
}

and this is class "Board" extends Component.这是 class “板”扩展组件。

I want to use different types of state.我想使用不同类型的 state。 ex) boardState, postState, pageState例如)boardState、postState、pageState

but when i use state.items in Board the error occurred.但是当我在 Board 中使用state.items时,发生了错误。

error message: TS2339: Property 'items' does not exist on type 'postState|错误消息:TS2339:类型 'postState| 上不存在属性 'items' boardState'.板州”。

how can I fix it?我该如何解决?

Can you just overr the type in the Board class?你能改写Board class 中的类型吗?

eg例如

interface boardState {
  items: string[];
  page: number;
}
interface postState {
  writer: string;
  content: string;
}
class Component {
  $target : Element;
  state: boardState | postState;
}

class Board extends Component{
  state: boardState;
  constructor() {
    super();
  }
  init(){
    this.state.items = ["title1", "title2"] ;
  }
}

https://typescript-bygs7m.stackblitz.io https://typescript-bygs7m.stackblitz.io

state can either be boardState or postState , not both state可以是boardStatepostState ,不能同时是

In the case that it is postState and you attempt to set the value of state.items , it will not work, so TypeScript throws an error.如果它是postState并且您尝试设置state.items的值,它将不起作用,因此 TypeScript 会引发错误。

that's because you only have items in boardState , what you need is do type narrowing and manage the case where items doesn't exist, in the TS documentation you can see more about that https://www.typescriptlang.org/docs/handbook/2/narrowing.html那是因为您在boardState中只有items ,您需要进行type narrowing并管理项目不存在的情况,在 TS 文档中您可以看到更多关于https://www.typescriptlang.org/docs/handbook /2/变窄.html

but in your case, I think is better add the member in the Board class但在你的情况下,我认为最好在董事会 class 中添加成员

暂无
暂无

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

相关问题 如何在Typescript中定义对象变量的类型? - How can I define the types of an object variable in Typescript? 如何在Typescript中使用定义为多种类型之一的变量来反对其中一种类型的数组? - How to use variable defined as one of multiple types against array of one of those types in Typescript? 如何在打字稿中使用条件导入模块的类型? - How can I use the types of a conditionally imported module in typescript? 如何在 Typescript 中将类型化变量用作具有相同名称的类型? - How can I use typed variables as types with same name in Typescript? 如何在 typescript 中为展平 object 键编写类型/或使用使用类型? - How to write types / or use use types for flatten object key in typescript? 我可以在 jsdoc 中使用 typescript 实用程序类型吗? - Can I use typescript utility types with jsdoc? 使用两个 Omit&lt;…&gt; | 键入的 React 组件道具省略&lt;...&gt; 引发 TS 错误 2339。Pick |上不存在属性 x 挑选 - React Component Props typed with two Omit<…> | Omit<…> throwing TS error 2339. Property x does not exist on Pick | Pick Typescript 无法编译类型不存在的错误 TS2339 - Typescript can't compile type not existing Error TS2339 是否可以将变量值用作Typescript中的联合类型之一? - Is it possible to use variable values as one of the union types in Typescript? 如何在TypeScript中导出多个对象实例? - How can I export multiple object instances in TypeScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM