网站建设玖首选金手指,企业官方网站推广,seo基础培训机构,做网站表格Partial Partial用于将给定类型的所有属性设置为可选。换句话说#xff0c;Partial 可以创建一个新的类型#xff0c;该类型具有与原始类型相同的属性#xff0c;但是这些属性都是可选的。使用 Partial 可以很方便地定义一个对象#xff0c;其中的属性可以选择性地进行赋值…Partial Partial用于将给定类型的所有属性设置为可选。换句话说Partial 可以创建一个新的类型该类型具有与原始类型相同的属性但是这些属性都是可选的。使用 Partial 可以很方便地定义一个对象其中的属性可以选择性地进行赋值。
interface User {name: string;age: number;email: string;
}function updateUser(user: PartialUser): void {// 更新用户信息// ...
}const user: User {name: John,age: 30,email: johnexample.com
};updateUser({ name: John Doe }); // 仅更新名称
updateUser({ age: 31, email: john.doeexample.com }); // 仅更新年龄和邮箱Required Required 用于将给定类型的所有属性设置为必需的。换句话说Required 可以创建一个新的类型该类型具有与原始类型相同的属性但是这些属性都是必需的不能省略。
interface User {name?: string;age?: number;email?: string;
}function createUser(user: RequiredUser): void {// 创建用户// ...
}createUser({ name: John, age: 30, email: johnexample.com }); // 完整的用户信息
createUser({ name: John }); // 缺少必需的属性会报错Readonly Readonly用于将给定类型的所有属性设置为只读。换句话说Readonly 可以创建一个新的类型该类型具有与原始类型相同的属性但是这些属性都是只读的不能被修改。使用 Readonly 可以很方便地定义一个只读的对象其中的属性不能被修改。这对于确保对象的不可变性和类型安全非常有用。
interface User {readonly name: string;readonly age: number;readonly email: string;
}function getUser(): ReadonlyUser {return { name: John, age: 30, email: johnexample.com };
}const user: ReadonlyUser getUser();console.log(user.name); // John
user.name John Doe; // 无法修改只读属性会报错Pick Pick 用于从给定类型中选择指定的属性并创建一个新的类型。换句话说Pick 可以从一个对象类型中挑选出指定的属性创建一个新的类型该类型只包含指定的属性。使用 Pick 可以很方便地从一个复杂的类型中选择需要的部分减少了不必要的冗余信息提高了代码的可读性和灵活性。
interface User {name: string;age: number;email: string;address: string;
}type UserBasicInfo PickUser, name | age;const user: UserBasicInfo {name: John,age: 30,
};Record Record 用于创建一个具有指定属性类型的对象类型。Record 接受两个类型参数第一个参数指定属性的名称第二个参数指定属性的类型。 使用 Record 可以很方便地定义一个具有指定属性类型的对象类型这对于创建字典、映射等数据结构非常有用。
type Fruit apple | banana | orange;
type Price number;const fruitPrices: RecordFruit, Price {apple: 1.5,banana: 0.5,orange: 0.8,
};console.log(fruitPrices.apple); // 1.5
console.log(fruitPrices.banana); // 0.5
console.log(fruitPrices.orange); // 0.8Exclude Exclude 用于从一个联合类型中排除指定的类型。 Exclude 接受两个类型参数第一个参数是要排除的类型第二个参数是要从中排除类型的联合类型。 下面是一个例子演示了如何使用 Exclude 类型工具
type Animal dog | cat | bird;
type ExcludeBird ExcludeAnimal, bird;
const myPets: ExcludeBird[] [dog, cat];Extract Extract 用于从一个联合类型中提取指定的类型。 Extract 接受两个类型参数第一个参数是要提取的类型第二个参数是要从中提取类型的联合类型。
type Animal dog | cat | bird;
type ExtractBird ExtractAnimal, bird;
const myBird: ExtractBird bird;Omit Omit 用于从一个对象类型中排除指定的属性。 Omit 接受两个类型参数第一个参数是要从中排除属性的对象类型第二个参数是要排除的属性的名称。
type Person {name: string;age: number;gender: string;
};type OmitAge OmitPerson, age;const personWithoutAge: OmitAge {name: John,gender: male
};NonNullable NonNullable 用于从一个类型中排除 null 和 undefined。 NonNullable 接受一个类型参数该参数表示要排除 null 和 undefined 的类型。
type NullableString string | null | undefined;
type NonNullableString NonNullableNullableString;
const str: NonNullableString Hello;Parameters Parameters 是一个泛型工具类型它用于获取函数类型 T 的参数类型。它接受一个函数类型作为参数并返回一个元组类型其中包含了函数的每个参数类型。
function greet(name: string, age: number): void {console.log(Hello, ${name}! You are ${age} years old.);
}可以使用Parameters来获取greet函数的参数类型
type GreetParams Parameterstypeof greet;
// GreetParams 的类型为 [string, number]ConstructorParameters ConstructorParameters 用于获取构造函数的参数类型。 ConstructorParameters 接受一个构造函数类型作为参数并返回一个元组类型该元组类型包含了构造函数的参数类型。
class Person {constructor(name: string, age: number) {// constructor implementation}
}
type PersonConstructorParams ConstructorParameterstypeof Person;
const params: PersonConstructorParams [John, 25];ReturnType ReturnType是一个泛型工具类型它用于获取函数类型T的返回值类型。它接受一个函数类型作为参数并返回该函数的返回值类型。
function add(a: number, b: number): number {return a b;
}可以使用ReturnType来获取add函数的返回值类型
type AddResult ReturnTypetypeof add;
// AddResult 的类型为 numberInstanceType InstanceType 用于获取构造函数的实例类型。 InstanceType 接受一个构造函数类型作为参数并返回该构造函数类型的实例类型。
class Person {name: string;age: number;constructor(name: string, age: number) {this.name name;this.age age;}sayHello() {console.log(Hello, my name is ${this.name} and Im ${this.age} years old.);}
}type PersonInstance InstanceTypetypeof Person;const person: PersonInstance new Person(John, 25);
person.sayHello();Uppercase Uppercase 用于将字符串类型的字母转换为大写。 Uppercase 接受一个字符串类型作为参数并返回该字符串类型的大写版本。
type UppercaseString Uppercasehello;
// UppercaseString 的类型为 HELLOconst str: UppercaseString HELLO;Lowercase Lowercase 用于将字符串类型的字母转换为小写。 Lowercase 接受一个字符串类型作为参数并返回该字符串类型的小写版本。
type LowercaseString LowercaseHELLO;
// LowercaseString 的类型为 helloconst str: LowercaseString hello;Capitalize Capitalize 用于将字符串的第一个字符转换为大写。
type MyString hello;
type CapitalizedString CapitalizeMyString;
// CapitalizedString 的类型为 HelloUncapitalize Uncapitalize用于将字符串的第一个字符转换为小写。
type MyString Hello;
type UncapitalizedString UncapitalizeMyString;
// UncapitalizedString 的类型为 hellohttps://www.zhihu.com/question/453332049/answer/3145262802