简体   繁体   English

类型“{}”缺少类型 ts(2739) 中的以下属性

[英]Type '{}' is missing the following properties from type ts(2739)

I have a function that makes structured data from rawData (from API)我有一个 function,它从rawData (来自 API)生成结构化数据

function makeData(raw:typeof rawData){
    const data:IData = {} // this line throws above error.

    const now = new Date()
    data.createdAt=now.toDateString();
    data.currentUser=raw.name;
    data.uniqueId= raw.id + now.toDateString();

    return data
}

As I am making the data, I am using an empty object in the beginning and typing it with IData so that the return value from the function is typed as IData .在制作数据时,我在开始时使用了一个空的 object 并使用 IData 键入它,以便将 function 的返回值键入为IData But as mentioned this is throwing error.但如前所述,这是抛出错误。

interface IData {
    createdAt:string;
    currentUser:string;
    uniqueId:string;
}

Usage:用法:

const {createdAt, currentUser,uniqueId} = makeData(rawData)

I tried to remove IData completely then I got the following error.我试图完全删除 IData 然后我收到以下错误。

Property 'createdAt' does not exist on type '{}'. // got the same error for other properties as well ( currentUser, uniqueId )

Getting the same error(s) on the line where destructing is done.在完成破坏的行上出现相同的错误。

I got a workaround for now:我现在有一个解决方法:

const data : Record<string,unknown>= {}

But this doesn't seem to be more convincing for me.但这对我来说似乎并不更有说服力。

Is there a better way to type data as IData.有没有更好的方法将数据键入为 IData。

Live Demo .现场演示

you can't define a const of IData without specify the data inside of it, instead you can do something like this如果不指定其中的数据,就不能定义IData的 const,相反,您可以这样做

function makeData(raw: typeof rawData): IData{
    const now = new Date()
    
    return {
        createdAt: now.toDateString(),
        currentUser: raw.name,
        uniqueId: raw.id + now.toDateString()
    }
    
}

Here as you are annotating data as IData .在这里,您将数据注释为IData It expects the object to contain all the required properties.它期望 object 包含所有必需的属性。 (in this case: createdAt, currentUser, uniqueId) (在这种情况下:createdAt、currentUser、uniqueId)

You can do 2 things.你可以做两件事。

1: You can do type assertion. 1:你可以做类型断言。

const data = {} as IData.

2: Initialise the object with empty values. 2:用空值初始化 object。

const data:IData = {
  createdAt:"",
  currentUser:"",
  uniqueId:""
 }

That's happen because all the propertys inside IData are required, so if you define a variable of type IData you need to provide it the values发生这种情况是因为IData中的所有属性都是必需的,因此如果您定义IData类型的变量,则需要为其提供值

Maybe you can use the UtilityType Partial or define what type are you going to return也许您可以使用 UtilityType Partial或定义您要返回的类型

function makeData(raw: typeof rawData): IData { }

The comment by @mikrowdev is the best solution for this i think. @mikrowdev 的评论是我认为的最佳解决方案。

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

相关问题 错误 TS2739:类型“{}”缺少类型中的以下属性 - error TS2739: Type '{}' is missing the following properties from type TS2739 类型“any[]”缺少来自“JSON”类型的以下属性:解析、字符串化、[Symbol.toStringTag] - TS2739 Type 'any[]' is missing the following properties from type 'JSON': parse, stringify, [Symbol.toStringTag] TS 错误:“事件”类型缺少“键盘事件”类型中的以下属性 - TS error: Type 'event' is missing the following properties from type 'keyboardevent' TS2740 缺少 type、Typescript、NestJS 中的以下属性 - TS2740 is missing the following properties from type, Typescript, NestJS Typescript:TS2739:缺少属性 - Typescript: TS2739: properties are missing 类型 &#39;Object&#39; 缺少类型 &#39;any[]&#39; 的以下属性:length、pop、push、concat 和 26 more.ts - Type 'Object' is missing the following properties from type 'any[]': length, pop, push, concat, and 26 more.ts 类型“文档”缺少类型中的以下属性 - Type 'Document' is missing the following properties from type 类型缺少类型的以下属性? - Type is missing the following properties from type? 类型“{}”缺少类型“IResponseConfig”中的以下属性 - Type '{}' is missing the following properties from type 'IResponseConfig' 错误:类型“{}”缺少类型中的以下属性 - Error: Type '{}' is missing the following properties from type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM