简体   繁体   English

如何描述 object 与 typescript 的唯一区别?

[英]How to describe object with the only difference in typescript?

I receive a pretty big object from serverside, let say我从服务器端收到了一个相当大的 object,比如说

{
 id:'asd123',
 created: 1591605861388,
 // hundred of fields more...  
}

My model type actualy is the same except the "created" - this is Date.我的 model 类型实际上是相同的,除了“创建” - 这是日期。

type Model = {
 id: string,
 created: Date,
 ...
}

I need to create function that creates model of type Model我需要创建 function 来创建 Model 类型的 model

function x(raw:???):Model{ // intentionally simplified
 return { 
    ...raw,
    created: new Date(raw.created) 
  }
} 

What i have to write instead of question marks?我必须写什么而不是问号? How can i define this in typescript?我如何在 typescript 中定义这个?

You can use Omit您可以使用省略

Playground 操场

type Model = {
 id: string,
 created: Date,
}

type Model2 = Omit<Model, 'created'> & {
    created: string;
}

const test: Model2 = {
    created: '2020-08-12',
    id: '123'
}

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

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