paint-brush
Amandla Ezinyunyana Ezicwaswayo ku-TypeScriptnge@tokenvolt
213 ukufundwa

Amandla Ezinyunyana Ezicwaswayo ku-TypeScript

nge Oleksandr Khrustalov4m2024/10/05
Read on Terminal Reader

Kude kakhulu; Uzofunda

Le ndaba ibukeza izisekelo zezinyunyana ezicwaswayo ku-TypeScrips. Ngizisebenzisa kaningi ekukhuleni kwami, ngakho-ke sizobheka isibonelo esithile sendlela yokuzisebenzisa.
featured image - Amandla Ezinyunyana Ezicwaswayo ku-TypeScript
Oleksandr Khrustalov HackerNoon profile picture

Esho inkinga

Njengoba i-typescript ikhula futhi ithola ukuduma muva nje, abathuthukisi be-javascript abaningi ngokwengeziwe bayakwazisa ukuphepha kohlobo. Uhlu lwezici ezihlinzekwa yi-Typescript lukhulu futhi lungase lube lukhulu, ngakho-ke kulokhu okuthunyelwe, ngizogxila kweyodwa yazo okulula ukuyiqonda futhi enomthelela omuhle ekusebenzeni.


Ake siqale ngesibonelo. Cabanga uthuthukisa uhlelo lokusebenza olunezindima eziningi zabasebenzisi. Kuyinto evamile ukuthi uhlelo lokusebenza ludliwe abasebenzisi abahlukene, akunjalo? Izindima azibalulekile ngempela lapha, kodwa ake sithi admin , consumer kanye guest . Ngombhalo wokubhala, singamemezela abasebenzisi ababambe lezo zindima ngale ndlela elandelayo:


 type Admin = {} type Consumer = {} type Guest = {}


Manje, ake sicabangele isethi yezibaluli indima ngayinye yomsebenzisi enazo. Ngokuvamile, email , firstName kanye lastName noma into efana naleyo. Kodwa, linda, Abasebenzisi Guest cishe ngeke babe nazo (izivakashi phela), ngakho-ke asivele silushiye lungenalutho lolu hlobo okwamanje.


 type Admin = { firstName: string lastName: string email: string } type Consumer = { firstName: string lastName: string email: string } type Guest = {}


Umsebenzisi wohlelo lokusebenza angaba nendima eyodwa kuphela. Indlela yokumela lokhu ngezinhlobo ukusebenzisa uhlobo union .


 type User = Admin | Consumer | Guest


Abaphathi badume ngamakhono abo akhethekile, futhi esicelweni sethu bayakwazi ukumema abathengi. Ake sengeze inkambu ebonisa ukuthi zingaki izimemo umlawuli angathumela.


 type Admin = { firstName: string lastName: string email: string numberOfInvitesLeft: number // <-- added }


Ukwenza izinto zihehe kakhudlwana futhi sisondele kuhlelo lokusebenza lwangempela, ake sengeze isakhiwo esikhethekile ohlotsheni Consumer .


 type Consumer = { firstName: string lastName: string email: string premium: boolean // <-- added }


Lesi isibonelo esilula kakhulu, futhi empeleni, abasebenzisi bangaba nenqwaba yezakhiwo ezihlukene, okwenza kube nzima kakhulu i-codebase uma udinga ukufinyelela izakhiwo ezithile.


 const doSomethingBasedOnRole = (user: User) => { // how do you check here that user is really an admin if (user) { // ...and do something with the `numberOfInvitesLeft` property? } }


Enye inketho ukuhlola ubukhona bendawo.


 const doSomethingBasedOnRole = (user: User) => { if (user && user.numberOfInvitesLeft) { // safely access `numberOfInvitesLeft` property } }


Kodwa lokhu kuyisixazululo esiyisicefe futhi akusona i-scalable. Futhi yini okufanele uyenze uma `numberOfInvitesLeft` iba indawo ozikhethela yona?

Sethula Izinhlobo Zenyunyana Ezicwaswayo

Yilapho izinhlobo zezinyunyana ezicwaswayo ziqala khona ukudlala. Sidinga nje ukubeka inkambu eyengeziwe kulo lonke uhlobo lomsebenzisi olubonisa indima.


 type Admin = { firstName: string lastName: string email: string numberOfInvitesLeft: number role: "admin" // <-- added } type Consumer = { firstName: string lastName: string email: string role: "consumer" // <-- added } type Guest = { role: "guest" // <-- added }


Qaphela ukuthi ngilubeka kanjani uchungechunge oluthile njengohlobo; lokhu kubizwa ngokuthi uhlobo lweyunithi yezinhlamvu . Lokhu okukunika kona ukuthi manje ungasebenzisa o-opharetha bolimi lwe-JS, isb , switch case , if , ukuze else indima.


 const user: Admin = { firstName: "John", lastName: "Smith", email: "[email protected]", numberOfInvitesLeft: 3, role: "admin", } const doSomethingBasedOnRole = (user: User) => { if (user.role === "admin") { // now typescript knows that INSIDE of this block user is of type `Admin` // now you can safely call `user.numberOfInvitesLeft` within this block } }


Okufanayo kuyasebenza esitatimendeni secala lokushintshwa.


 // ... const doSomethingBasedOnRole = (user: User) => { switch (user.role) { case "admin": { // now typescript knows that INSIDE of this block user is of type `Admin` // now you can safely call `user.numberOfInvitesLeft` within this block } case "consumer": { // do something with a `Consumer` user // if you try to call `user.numberOfInvitesLeft` here, TS compiler errors in // // "Property 'numberOfInvitesLeft' does not exist on type 'Consumer'." // } } }


Izinzuzo zezinhlobo zezinyunyana ezicwaswayo zisobala ngenxa yokuthi ukuhlola kohlobo kusekelwe endimeni yendima esobala futhi hhayi ezakhiweni zesikhangiso okungenzeka noma ezingahlobene nomsebenzisi othile.