gpt4 book ai didi

Typescript 自动获取类中的接口(interface)属性

转载 作者:行者123 更新时间:2023-12-04 18:00:58 33 4
gpt4 key购买 nike

你好 TypeScript 专家。

我有以下代码,但我必须在类中重复接口(interface)属性,否则我会得到:

Class incorrectly implements interface



使用接口(interface)时,是否有 TypeScript 速记来执行此操作而无需声明 Id: number;以及类中的所有其他属性?谢谢
interface INavigation {
Id: number;
AppId: number;
NavId: number;
Name: string;
ParentId: string;
PageURL: string;
Position: string;
Active: string;
Desktop: string;
Tablet: string;
Phone: string;
RoleId: string;
Target: string;
}

class Navigation implements INavigation {

Id: number;
AppId: number;
NavId: number;
Name: string;
ParentId: string;
PageURL: string;
Position: string;
Active: string;
Desktop: string;
Tablet: string;
Phone: string;
RoleId: string;
Target: string;

constructor(navigation: any) {
this.Id = navigation.Id
this.AppId = navigation.NavAppId
this.NavId = navigation.NavId
this.Name = navigation.NavName
this.ParentId = navigation.NavParentId
this.PageURL = navigation.NavPageURL
this.Position = navigation.NavPosition
this.Active = navigation.NavActive
this.Desktop = navigation.NavDesktop
this.Tablet = navigation.NavTablet
this.Phone = navigation.NavPhone
this.RoleId = navigation.NavRoleId
this.Target = navigation.NavTarget
}
}

最佳答案

现在可以在 Typescript 中使用类/接口(interface)合并。

interface Foo {
a: number;
}

interface Baz extends Foo { }
class Baz {
constructor() {
console.log(this.a); // no error here
}
}

https://github.com/Microsoft/TypeScript/issues/340#issuecomment-184964440

关于Typescript 自动获取类中的接口(interface)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53128744/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com