简体   繁体   English

`Object.entries().map()` 不会在打字稿上保留类型。 如何改进我的界面以使其正常工作? 或解决方法?

[英]`Object.entries().map()` don't preserve types on typescript. How can I improve my interface for it to work? or workaround?

I have this interface that i am working with:我有我正在使用的这个界面:

export interface NPMPackage {
  name: string;
  description: string;
  'dist-tags': {
    [tag: string]: string;
  };
  versions: {
    [version: string]: {
      name: string;
      version: string;
      dependencies?: {
        [packageName: string]: string;
      };
    };
  };
}

and this is my code:这是我的代码:

let dependencies =  npmPackage.versions[version].dependencies;
dependencies = Object.entries(dependencies).map( ([name, version]) => ({
    name,
    version: version.replace(/[^\w\s]/gi, ''),
}));

that returns these errors:返回这些错误:

Type '{ name: string;输入'{名称:字符串; version: string;版本:字符串; }[]' is not assignable to type '{ [packageName: string]: string; }[]' 不可分配给类型 '{ [packageName: string]: string; }'. }'。 Index signature is missing in type '{ name: string;类型 '{ name: string; 中缺少索引签名; version: string;版本:字符串; }[]'. }[]'。

No overload matches this call.没有重载匹配这个调用。 Overload 1 of 2, '(o: { [s: string]: string; } | ArrayLike): [string, string][]', gave the following error.重载 1 of 2, '(o: { [s: string]: string; } | ArrayLike): [string, string][]',给出以下错误。 Argument of type '{ [packageName: string]: string; '{ [packageName: string]: string; 类型的参数} | } | undefined' is not assignable to parameter of type '{ [s: string]: string; undefined' 不能分配给 '{ [s: string]: string; 类型的参数。 } | } | ArrayLike'. ArrayLike'。 Type 'undefined' is not assignable to type '{ [s: string]: string;类型 'undefined' 不能分配给类型 '{ [s: string]: string; } | } | ArrayLike'. ArrayLike'。 Overload 2 of 2, '(o: {}): [string, any][]', gave the following error.重载 2 of 2, '(o: {}): [string, any][]',出现以下错误。 Argument of type '{ [packageName: string]: string; '{ [packageName: string]: string; 类型的参数} | } | undefined' is not assignable to parameter of type '{}'. undefined' 不能分配给类型为 '{}' 的参数。 Type 'undefined' is not assignable to type '{}'.类型“未定义”不能分配给类型“{}”。

without the dependencies function it works with no errors.如果没有dependencies功能,它就可以正常工作没有错误。

if i add dependencies:any and change the versions variable without manipulating it like this:如果我添加dependencies:any并更改versions变量而不像这样操作它:

    let dependencies =  npmPackage.versions[version].dependencies;
    // lets make it pretty. 
    dependencies = Object.entries(dependencies).map( ([name, version]) => ({
        name,
        version,
    }));

it works.有用。

basically what i am trying to do is to add the dependencies method to change the structure of my object.基本上我想做的是添加依赖项方法来改变我的对象的结构。 the structure of the objects is passed to the new variable dependencies .对象的结构被传递给新的变量dependencies what is the proper solution for these cases with typescript?使用打字稿处理这些情况的正确解决方案是什么? is that considered a good practice when typehinting?打字提示时,这被认为是一个好习惯吗?

The api returns the data as described, but i want to send it with a different structure for the react frontend app. api 返回所描述的数据,但我想为反应前端应用程序以不同的结构发送它。

also.还。 the thing that i do not understand is - that the variable is a string as stated in the interface, so why doesn't it work?我不明白的是 - 变量是接口中所述的字符串,那么为什么它不起作用?

reproducible 可重复的

I want to change the structure of my object.我想改变我的对象的结构。 The structure of the objects is passed to the new variable dependencies .对象的结构被传递给新的变量dependencies

No, it's not.不,这不对。 You're assigning it to the old variable dependencies , which is inferred to have the old structure as its type.您将它分配给旧的变量dependencies ,它被推断为具有旧的结构作为其类型。

what is the proper solution for these cases with typescript?使用打字稿处理这些情况的正确解决方案是什么?

Use two different variables for different objects with different structures.对具有不同结构的不同对象使用两个不同的变量。 One is a object mapping strings to strings, the other is an array of objects.一个是将字符串映射到字符串的对象,另一个是对象数组。 So write所以写

const dependenciesObj = npmPackage.versions[version].dependencies;
// lets make it pretty. 
const dependenciesArr = Object.entries(dependencies).map( ([name, version]) => ({
    name,
    version,
}));

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

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