简体   繁体   English

在 Typescript 中获取自动完成的显式键名

[英]Get explicit key names for autocompletion in Typescript

What I want to do is given an object with a certain type, pass it through a function, completely remove either mobile or web parts depending on which one I tell it to remove and then also remove the key of the one I want to keep and shift its contents up below the mobile/web parent.我想要做的是给定一个特定类型的 object,将其传递给 function,完全删除mobileweb部件,具体取决于我告诉它保留哪个部件,然后删除将其内容向上移动到移动/网络父级下方。 This image will demonstrate this.这张图片将证明这一点。 在此处输入图像描述 Ive simplified the example to reduce the code, so if it seems pointless that may be why.我已经简化了示例以减少代码,所以如果它看起来毫无意义,那可能就是原因。

So then when I have the object it should look like this因此,当我拥有 object 时,它应该看起来像这样

const newObj: NewTypeCreated = {
  key1: {
    width: 150,
    height: 400,
  },
  key2: {
    width: 100,
  },
};

I already have this working with this code我已经使用此代码

const customComponentsStyle = (
  comps: ResponsiveCustomComponentsStyle,
  mobileOrWeb: 'mobile' | 'web',
) =>
  Object.keys(comps).reduce((newObj, currentKey) => {
    const { width, height, maxHeight } = customComponents[currentKey][mobileOrWeb]!;
    const newObject = {
      ...(width && { width }),
      ...(height && { height }),
      ...(maxHeight && { maxHeight }),
    };

    return {
      ...newObj,
      [currentKey]: newObject,
    };
  }, {});

const x = customComponentsStyle(customComponents, 'mobile');
console.log(x);

//Output
{ key1: { width: 150, height: 400 }, key2: { width: 100 } }

The issue is there is no autocompletion on x it has no idea what is on it.问题是x上没有自动完成功能,它不知道上面有什么。

console.log(x.key1.width);

ts error
Property 'key1' does not exist on type '{}'.ts(2339)

Can anyone point me in the right direction here?谁能在这里指出我正确的方向? I just want to be able to do x and have proper auto completion based on the new objects type.我只想能够执行 x 并根据新的对象类型进行适当的自动完成。

You could explicitly tell Typescript that customComponentsStyle returns NewTypeCreated type of data:您可以明确告诉 Typescript customComponentsStyle返回NewTypeCreated类型的数据:

const customComponentsStyle = (
  comps: ResponsiveCustomComponentsStyle,
  mobileOrWeb: 'mobile' | 'web',
): NewTypeCreated => {
  ...
}

This will tell Typescript that customComponentsStyle returns an object with string properties, where each property is an object with optional numbers in width and height .这将告诉 Typescript customComponentsStyle返回一个带有字符串属性的 object ,其中每个属性都是一个 object , widthheight为可选数字。

You could add these types below您可以在下面添加这些类型

type KeyType = {
  width?: number
  height?: number
}

type ReturnType = {
  key1?: KeyType
  key2?: KeyType
}

and change the customComponentsStyle function like this code screenshot并更改customComponentsStyle function 像这个代码截图

const customComponentsStyle = (
  comps: ResponsiveCustomComponentsStyle,
  mobileOrWeb: 'mobile' | 'web',
): ReturnType =>
  Object.keys(comps).reduce((newObj, currentKey) => {
    const { width, height, maxHeight } = customComponents[currentKey][mobileOrWeb]!;
    const newObject = {
      ...(width && { width }),
      ...(height && { height }),
      ...(maxHeight && { maxHeight }),
    };

    return {
      ...newObj,
      [currentKey]: newObject,
    };
  }, {} as ReturnType);

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

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