如何在 TypeScript 中同时使用 'Pick' 和 '&' 关键字声明数组对象的类型
How do I declare a type for array object using 'Pick' and '&' keyword at the same time in TypeScript
我是 TypeScript 的新手,我想要的是为数组对象声明一个类型,如下所示。
const temp = [{
id: 0, // number
follower_id: 0, // number
followee_id: 0, // number
created_at: '', // string
delete_at: '', // string
}, {
id: 1,
follower_id: 1,
followee_id: 0,
created_at: '',
delete_at: '',
}];
我可以为临时数组创建一个新类型,但我只想重用我之前定义的类型,这样我就可以避免重复无意义的输入。
所以,这是我制作的类型。 userStateType 的两个接口。
interface IUserInfo {
id: number;
created_at: string;
deleted_at: string | null;
username: string;
description: string;
image_url: string;
name: string;
updated_at: string;
}
interface IFollows {
follower_id: number,
followee_id: number
}
我试着像下面这样写下我的 UserStateType。
type UserStateType = {
userInfo: IUserInfo | null
followers: Pick<IUserInfo, 'created_at' | 'deleted_at'> & IFollows | null
};
但是UserStateType中的followers必须是数组类型而不是对象类型所以会出现类型错误。 (目前,我正在使用这种类型来避免类型错误。followers: [] | null
)
我什至无法使用 []
,因为我不仅使用了 Pick
关键字,而且还使用了 &
。
在这种情况下,我该如何想出有效的解决方案?
如果我解释的不够清楚,欢迎评论!
你非常。你只需要把它变成一个数组:
followers: (Pick<IUserInfo, 'created_at' | 'deleted_at'> & IFollows)[] | null
或者您可以制作一个 Follower
类型并使用它:
type Follower = Pick<IUserInfo, 'created_at' | 'deleted_at'> & IFollows;
type UserStateType = {
userInfo: IUserInfo | null;
followers: Follower[] | null;
};
你只是缺少括号:
interface IUserInfo {
id: number;
created_at: string;
deleted_at: string | null;
username: string;
description: string;
image_url: string;
name: string;
updated_at: string;
}
interface IFollows {
follower_id: number,
followee_id: number
}
type UserStateType = {
userInfo: IUserInfo | null
followers: (Pick<IUserInfo, 'created_at' | 'deleted_at'> & IFollows)[]
};
然后你可以做:
const example:UserStateType = { followers:{created_at:'',deleted_at:'',followee_id:0,follower_id:0}], userInfo:null}
我是 TypeScript 的新手,我想要的是为数组对象声明一个类型,如下所示。
const temp = [{
id: 0, // number
follower_id: 0, // number
followee_id: 0, // number
created_at: '', // string
delete_at: '', // string
}, {
id: 1,
follower_id: 1,
followee_id: 0,
created_at: '',
delete_at: '',
}];
我可以为临时数组创建一个新类型,但我只想重用我之前定义的类型,这样我就可以避免重复无意义的输入。 所以,这是我制作的类型。 userStateType 的两个接口。
interface IUserInfo {
id: number;
created_at: string;
deleted_at: string | null;
username: string;
description: string;
image_url: string;
name: string;
updated_at: string;
}
interface IFollows {
follower_id: number,
followee_id: number
}
我试着像下面这样写下我的 UserStateType。
type UserStateType = {
userInfo: IUserInfo | null
followers: Pick<IUserInfo, 'created_at' | 'deleted_at'> & IFollows | null
};
但是UserStateType中的followers必须是数组类型而不是对象类型所以会出现类型错误。 (目前,我正在使用这种类型来避免类型错误。followers: [] | null
)
我什至无法使用 []
,因为我不仅使用了 Pick
关键字,而且还使用了 &
。
在这种情况下,我该如何想出有效的解决方案?
如果我解释的不够清楚,欢迎评论!
你非常。你只需要把它变成一个数组:
followers: (Pick<IUserInfo, 'created_at' | 'deleted_at'> & IFollows)[] | null
或者您可以制作一个 Follower
类型并使用它:
type Follower = Pick<IUserInfo, 'created_at' | 'deleted_at'> & IFollows;
type UserStateType = {
userInfo: IUserInfo | null;
followers: Follower[] | null;
};
你只是缺少括号:
interface IUserInfo {
id: number;
created_at: string;
deleted_at: string | null;
username: string;
description: string;
image_url: string;
name: string;
updated_at: string;
}
interface IFollows {
follower_id: number,
followee_id: number
}
type UserStateType = {
userInfo: IUserInfo | null
followers: (Pick<IUserInfo, 'created_at' | 'deleted_at'> & IFollows)[]
};
然后你可以做:
const example:UserStateType = { followers:{created_at:'',deleted_at:'',followee_id:0,follower_id:0}], userInfo:null}