Typescript自动获取类中的接口属性
您好,打字机专家 我有以下代码,但我必须在类中重复接口属性,否则我会得到: 类错误地实现了接口 当使用接口时,是否有一个TypeScript速记来完成此操作,而不必声明Typescript自动获取类中的接口属性,typescript,Typescript,您好,打字机专家 我有以下代码,但我必须在类中重复接口属性,否则我会得到: 类错误地实现了接口 当使用接口时,是否有一个TypeScript速记来完成此操作,而不必声明Id:number和类中的所有其他属性?Thx interface INavigation { Id: number; AppId: number; NavId: number; Name: string; ParentId: string; PageURL: string; Position: str
Id:number代码>和类中的所有其他属性?Thx
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
}
}
对此没有内置的支持
但是,我们可以使用返回类作为类的基类型的函数。这个函数可能有点虚假,并声称它实现了接口。如有必要,我们还可以为成员传递一些默认值
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;
}
function autoImplement<T>(defaults?: Partial<T>) {
return class {
constructor() {
Object.assign(this, defaults || {});
}
} as new () => T
}
class Navigation extends autoImplement<INavigation>() {
constructor(navigation: any) {
super();
// other init here
}
}
接口失活{
Id:编号;
AppId:编号;
NavId:数字;
名称:字符串;
ParentId:string;
PageURL:字符串;
位置:字符串;
活动:字符串;
桌面:字符串;
碑:弦;
电话:字符串;
RoleId:字符串;
目标:字符串;
}
函数自动执行(默认值?:部分){
返回舱{
构造函数(){
assign(这是默认值| |{});
}
}as new()=>T
}
类导航扩展了autoImplement(){
构造函数(导航:任意){
超级();
//这里的其他初始化
}
}
如果我们想要有一个基类,事情会变得更复杂,因为我们必须对基类执行一些操作:
function autoImplementWithBase<TBase extends new (...args: any[]) => any>(base: TBase) {
return function <T>(defaults?: Partial<T>): Pick<TBase, keyof TBase> & {
new(...a: (TBase extends new (...o: infer A) => unknown ? A : [])): InstanceType<TBase> & T
} {
return class extends base {
constructor(...a: any[]) {
super(...a);
Object.assign(this, defaults || {});
}
} as any
}
}
class BaseClass {
m() { }
foo: string
static staticM() { }
static staticFoo: string
}
class Navigation extends autoImplementWithBase(BaseClass)<INavigation>() {
constructor(navigation: any) {
super();
// Other init here
}
}
Navigation.staticFoo
Navigation.staticM
new Navigation(null).m();
new Navigation(null).foo;
函数autoImplementWithBase any>(base:TBase){
返回函数(默认值?:部分):拾取和{
new(…a:(TBase扩展new(…o:推断a)=>未知?a:[]):InstanceType&T
} {
返回类扩展了基{
构造函数(…a:任何[]){
超级(…a);
assign(这是默认值| |{});
}
}一样
}
}
类基类{
m(){}
foo:string
静态staticM(){}
静态静态foo:string
}
类导航扩展了autoImplementWithBase(基类)(){
构造函数(导航:任意){
超级();
//这里的其他初始化
}
}
Navigation.staticFoo
导航静态
新导航(null).m();
新导航(null).foo;
类声明应该显式地实现接口。这现在可以在Typescript中使用类/接口合并
interface Foo {
a: number;
}
interface Baz extends Foo { }
class Baz {
constructor() {
console.log(this.a); // no error here
}
}
两个答案的混合,通过在构造函数中使用类/接口合并和Object.assign,避免在构造函数中长时间赋值:
interface Foo {
a: number;
b: number;
}
interface Baz extends Foo { }
class Baz {
c: number = 4
constructor (foo: Foo) {
Object.assign(this, foo, {})
}
getC() {
return this.c
}
}
let foo: Foo = {
a: 3,
b: 8
}
let baz = new Baz(foo)
// keep methods and properties
console.warn(baz)
不知道这意味着什么,你能提供一个例子吗?请或详细说明-thxit意味着你应该实现你想说的界面中的所有成员?很抱歉,我弄错了,是的,明确地说,尽管下面的答案非常好,如果没有其他类为tip实现相同的interfacethanks,那么可能根本没有必要实现接口。这应该是选择的答案这确实会将接口的所有属性添加到类中,但是如何避免构造函数中的大量赋值?没有我所希望的那样有用,但是比“踢裤子+1”要好