TypeScript-----------------------------高阶知识点映射类型
·
//基础映射类型
// interface User10
// {
// readonly name:string;
// readonly age:number;
// male?:boolean;
// }
// type FilterReadOnly<Type> = {
// - readonly [Property in keyof Type]:Type[Property];
// //- readonly [Property in keyof Type] - ?:Type[Property];
// }
// type PublicUser10 = FilterReadOnly<User10>;
// const publicUser:PublicUser10 = {
// name:"dell",
// age:30,
// male:true,
// }
// publicUser.age = 31;
//映射类型高级语法
// interface User10
// {
// readonly name:string;
// readonly age:number;
// male:boolean;
// }
// type DeleteMaleProperty<Type> = {
// [Property in keyof Type as Exclude<Property,'male'>]:Type[Property];
// }
// type UserWithoutGender = DeleteMaleProperty<User10>;
// const user10:UserWithoutGender = {
// name:"dell",
// age:30,
// }
//2.字面量语法例子
interface User10
{
name:string;
age:number;
male:boolean;
sex:string;
}
interface UserFunctions{
getName:()=>string;
getAge:()=>number;
getMale:()=>boolean;
}
type GetPropertyFunctions<Type> = {
[Property in keyof Type as `get${Capitalize<string & Property>}`]:()=>Type[Property];
}
type UserFunctionsType = GetPropertyFunctions<User10>;
//3.union类型使用
type SquareEvent = {
kind:'square';
x: number;
y: number;
}
type CircleEvent = {
kind:'circle';
radius: number;
}
type GenerateEventsFunctions<Events extends{ kind:string}>={
[Event in Events as Event['kind']]:(event:Event)=>number;
}
type NewType = GenerateEventsFunctions<SquareEvent|CircleEvent>;
更多推荐

所有评论(0)