简体   繁体   English

在 Typescript 中指定参数类型的值

[英]Specify values of a parameter type in Typescript

I have these interfaces:我有这些接口:

interface IComponent {
  type: string;
  text: string;
}

interface IComponents {
  cc: IComponent;
  lajota: IComponent;
}

interface IMain {
  components: IComponents
}

And it is working fine!它工作正常! But now I need to add a new component called "caneta".但现在我需要添加一个名为“caneta”的新component
SO I'll access this with .components.caneta .所以我将使用.components.caneta访问它。 But this new component will have a single attribute:但是这个新组件只有一个属性:

interface IComponentCaneta {
  property: string;
}  

// Add the new component to be used on IMain
interface IComponents {
  cc?: IComponent;
  lajota?: IComponent;
  caneta?: IComponentCaneta;
}

The problem is I have a method that do some work depending on the attribute type like:问题是我有一个方法可以根据属性type做一些工作,例如:

//for each component I have in components objects 
function myFunc(component: IComponent) {
_.each(components (val, key) => {
  if (key === 'cc') {...}
  else if (value?.type === 'xxx') {  <---- HERE flags error
    components[key].type = 'xxxx'
  }
})
}

When I add the new component caneta , Typescript complains saying:当我添加新组件caneta时, Typescript抱怨说:

Property 'type' does not exist on type 'IComponentCaneta'.类型“IComponentCaneta”上不存在属性“类型”。

Tried to make type optional, but didn't work.试图使type可选,但没有用。

What would be the right thing to do in this situation?在这种情况下,正确的做法是什么?
Is there a way to explicitly say that "The attribute of type IComponent will be 'X' 'Y' or 'Z'. Something like有没有办法明确地说“IComponent 类型的属性将是‘X’‘Y’或‘Z’。像

function myFunc(component: IComponent ['cc' or 'lajota'])

Things I tried and failed:我尝试过但失败了的事情:

// make type optional
interface IComponent {
  type?: string;
  text: string;
}


// try to infer the object (cc, loja, caneta)
switch (type) {
  case 'cc':
    // ...
    break;
  case 'lajota':
    // ...
    break;
  default: //'caneta'
    // ...
    break;
}

//using IF/ELSE 
if (type === 'cc') {.../}
else if(type === 'lajota') {...}
else if(type === 'caneta') {...}

I found a solution using Object Entries and forEach .我找到了一个使用Object EntriesforEach的解决方案。
I don't know if there is a "down side" yet.我不知道是否还有“缺点”。 I just wanted a way to iterate through the components and make typescript happy.我只是想要一种遍历组件并使打字稿快乐的方法。
The only solution I could think of was try to infer the object so TS could "see" the right attributes.我能想到的唯一解决方案是尝试推断对象,以便 TS 可以“看到”正确的属性。

function myFunc (components: IComponents) {
    Object.entries(components).forEach(([key, value], index) => {
        if (key === 'caneta') {
            components[key].property = 'Hello World';
        } else if(value?.type === 'text') {  <--- No longer gives me errors
            components[key].type = 'NEW TYPE'
        }
    });
}

One thing that kind worries me is that when I was trying this code on Typescript Playground it gave me the following error/warning:让我担心的一件事是,当我在Typescript Playground上尝试这段代码时,它给了我以下错误/警告:

Object is possibly 'undefined'.对象可能是“未定义的”。

on the following lines:在以下几行:

components[key].property = 'Hello World';

and
components[key].type = 'NEW TYPE'

No errors on my vscode/lint though虽然我的 vscode/lint 没有错误

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

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