新聞中心
TypeScript 是 JavaScript 的超集,它為該語言添加了靜態(tài)類型和其他功能。TypeScript 最強(qiáng)大的功能之一是它對(duì)實(shí)用函數(shù)的支持,它允許開發(fā)人員以強(qiáng)大的方式操作類型。

創(chuàng)新互聯(lián)自2013年起,先為洪雅等服務(wù)建站,洪雅等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為洪雅企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
在本文中,我們將分享6個(gè)開發(fā)者必須知道的 TypeScript 實(shí)用函數(shù),并為每個(gè)函數(shù)提供示例和解釋。
01、Partial
Partial函數(shù)允許您通過將原始類型 T 的所有屬性設(shè)為可選來創(chuàng)建新類型。當(dāng)您有一個(gè)具有許多屬性的復(fù)雜類型時(shí),這會(huì)很有用,但您只需要在特定的上下文中提供其中的一些。
例如,假設(shè)您有一個(gè)具有多個(gè)必需屬性的 Person 接口:
interface Person {
firstName: string;
lastName: string;
age: number;
address: Address;
}
interface Address {
street: string;
city: string;
state: string;
zip: string;
}如果你想創(chuàng)建一個(gè)新類型,它具有與 Person 相同的屬性,但所有屬性都是可選的,你可以像這樣使用 Partial 效用函數(shù):
type PartialPerson = Partial; 現(xiàn)在您可以創(chuàng)建一個(gè) PartialPerson 類型的變量,并且只提供您需要的屬性:
const partialPerson: PartialPerson = {
firstName: 'John',
lastName: 'Doe'
};02、Required
Required 實(shí)用程序函數(shù)通過使原始類型 T 的所有屬性成為必需來創(chuàng)建新類型。當(dāng)您有一個(gè)帶有可選屬性的類型,但您希望確保在特定上下文中提供所有屬性時(shí),這可能很有用。
繼續(xù)前面示例中的 Person 接口,假設(shè)您要?jiǎng)?chuàng)建一個(gè)新類型,該類型具有 Person 所需的所有屬性。您可以像這樣使用 Required 實(shí)用程序函數(shù):
type RequiredPerson = Required; 現(xiàn)在您可以創(chuàng)建 RequiredPerson 類型的變量并確保提供所有屬性:
const requiredPerson: RequiredPerson = {
firstName: 'John',
lastName: 'Doe',
age: 30,
address: {
street: '123 Main St',
city: 'Anytown',
state: 'CA',
zip: '12345'
}
};03、Readonly
Readonly 實(shí)用程序函數(shù)通過將原始類型 T 的所有屬性設(shè)置為只讀來創(chuàng)建新類型。當(dāng)您有一個(gè)在創(chuàng)建后不應(yīng)修改的類型時(shí),這會(huì)很有用。
例如,假設(shè)您有一個(gè)具有多個(gè)屬性的用戶界面:
interface User {
id: number;
username: string;
email: string;
password: string;
}如果你想創(chuàng)建一個(gè)新類型,它具有與 User 相同的屬性,但所有屬性都是只讀的,你可以像這樣使用 Readonly 實(shí)用函數(shù):
type ReadonlyUser = Readonly; 現(xiàn)在你可以創(chuàng)建一個(gè) ReadonlyUser 類型的變量,并確保它的任何屬性都不能被修改:
const user: ReadonlyUser = {
id: 1,
username: 'johndoe',
email: 'john.doe@example.com',
password: 'password123'
};
user.id = 2; // Error: Cannot assign to 'id' because it is a read-only property.04、Record
Record 實(shí)用程序函數(shù)通過將類型 K 的鍵映射到類型 T 的值來創(chuàng)建新類型。當(dāng)您要?jiǎng)?chuàng)建表示具有特定鍵的一組值的類型時(shí),這會(huì)很有用。
例如,假設(shè)您有一組要表示為對(duì)象的錯(cuò)誤代碼,其中鍵是錯(cuò)誤代碼,值是錯(cuò)誤消息:
type ErrorCode = 'E001' | 'E002' | 'E003';
type ErrorMessages = Record;
const errorMessages: ErrorMessages = {
E001: 'An error occurred.',
E002: 'Another error occurred.',
E003: 'Yet another error occurred.'
}; 現(xiàn)在您可以通過相應(yīng)的錯(cuò)誤代碼訪問錯(cuò)誤消息:
const errorMessage = errorMessages['E001']; // 'An error occurred.'05、Exclude
Exclude 實(shí)用程序函數(shù)通過從類型 T 中排除聯(lián)合 U 中的所有類型來創(chuàng)建新類型。這在您想要定義僅包含某些類型的類型時(shí)非常有用。
例如,假設(shè)您的 Color 類型包含多個(gè)可能的顏色值:
type Color = 'red' | 'green' | 'blue' | 'yellow';如果你想定義一個(gè)排除某些顏色值的類型,你可以像這樣使用 Exclude 實(shí)用函數(shù):
type PrimaryColor = Exclude; 現(xiàn)在您可以創(chuàng)建一個(gè) PrimaryColor 類型的變量并確保它只包含原色:
const primaryColor: PrimaryColor = 'red'; // OK
const secondaryColor: PrimaryColor = 'green'; // Error: Type 'green' is not assignable to type 'PrimaryColor'.06、Omit
Omit 實(shí)用程序函數(shù)通過從原始類型 T 中省略指定的屬性 K 來創(chuàng)建新類型。當(dāng)您有一個(gè)具有許多屬性的類型,但您想創(chuàng)建一個(gè)沒有某些屬性的新類型時(shí),這會(huì)很有用。
例如,假設(shè)您有一個(gè)具有多個(gè)屬性的用戶界面:
interface User {
id: number;
username: string;
email: string;
password: string;
}如果你想創(chuàng)建一個(gè)新的類型,它具有除密碼之外的用戶的所有屬性,你可以使用 Omit 實(shí)用函數(shù),如下所示:
type UserWithoutPassword = Omit; 現(xiàn)在您可以創(chuàng)建一個(gè) UserWithoutPassword 類型的變量,而無需提供密碼屬性:
const userWithoutPassword: UserWithoutPassword = {
id: 1,
username: 'johndoe',
email: 'john.doe@example.com',
};總之,TypeScript 實(shí)用程序函數(shù)是開發(fā)人員的強(qiáng)大工具,允許更靈活和可重用的代碼。
通過有效地理解和使用這些實(shí)用函數(shù),開發(fā)人員可以創(chuàng)建更健壯和可維護(hù)的 TypeScript 代碼。這些功能只是 TypeScript 語言的一小部分,但它們會(huì)對(duì)您的代碼質(zhì)量和開發(fā)團(tuán)隊(duì)的工作效率產(chǎn)生重大影響。
文章題目:六個(gè) TypeScript 實(shí)用函數(shù)
分享URL:http://www.5511xx.com/article/cohhphg.html


咨詢
建站咨詢
