Fitattun fasalulluka na TypeScript: Tsayawa Nau'in Checking, Compiler, Module System

Taimakon Taimako Nau'in A tsaye

Ɗayan ƙarfinsa shine TypeScript ikonsa na yin duban nau'in a tsaye. Tare da wannan fasalin, za mu iya ayyana da amfani da nau'ikan bayanai zuwa masu canji, sigogin aiki, da ƙimar dawowa.

Misali:

let age: number = 25;  
let name: string = "John";  
let isActive: boolean = true;  

A cikin misalin da ke sama, muna ayyana masu canji age na nau'in number, name na nau'in string, da isActive na nau'in boolean. TypeScript zai duba ingancin ayyuka kuma ya ba da rahoton kurakurai idan an sami sabani.

 

Mai tarawa da Tallafin Automation

TypeScript ya zo tare da mai tarawa mai ƙarfi wanda ke jujjuya TypeScript lamba zuwa JavaScript lamba daidai. Bugu da ƙari, TypeScript yana ba da kayan aikin sarrafa kansa don ayyuka kamar gyara kurakurai, tsara lamba, da bincikar ma'amala, haɓaka haɓaka aiki da rage ƙoƙari yayin haɓakawa.

Misali:

// TypeScript code  
const sum =(a: number, b: number): number => {  
  return a + b;  
};  
  
// Transpiled JavaScript code  
var sum = function(a, b) {  
  return a + b;  
};  

 

Duban Kuskuren Tsara-Lokaci

TypeScript yana aiwatar da binciken kuskure a lokacin tattarawa, gano kurakurai masu ma'ana, kurakuran daidaitawa, da batutuwa masu alaƙa kafin gudanar da aikace-aikacen.

Misali:

const calculateArea =(radius: number): number => {  
  return Math.PI * radius * radius;  
};  
  
console.log(calculateArea("5")); // Lỗi: kiểu dữ liệu không phù hợp  

A cikin misalin da ke sama, TypeScript zai kama kuskuren yayin haɗawa yayin da muke wuce kirtani "5" zuwa ma'auni radius na nau'in number.

 

Module Tallafin tsarin

TypeScript yana goyan bayan tsari mai ƙarfi module, yana ba da damar rarraba lambar tushe zuwa kayayyaki masu zaman kansu. Wannan yana haɓaka sarrafa lambar, sake amfani da shi, da haɓakawa.

Misali:

// Module A  
export const greeting = "Hello";  
  
// Module B  
import { greeting } from "./moduleA";  
console.log(greeting); // Kết quả: "Hello"  

A cikin misalin da ke sama, muna da kayayyaki guda biyu, moduleA da moduleB. moduleA yana fitar da mabambanta greeting, kuma moduleB yana shigo da greeting canjin daga moduleA kuma yana amfani da shi.

 

Extended Syntax and Features

TypeScript yana faɗaɗa juzu'i da fasali na JavaScript. Misali, TypeScript yana goyan bayan sabbin ECMAScript fasaloli kamar ayyukan kibiya, async/jira, lalatawa, da ainihin samfuri. Wannan yana ba masu haɓaka damar yin amfani da fasalulluka na zamani kuma su rubuta ƙarin lambar da za a iya karantawa da fahimta.

Misali:

const name = "John";  
const message = `Hello, ${name}! Welcome to TypeScript.`;  
console.log(message); // Kết quả: "Hello, John! Welcome to TypeScript."  

A cikin misalin da ke sama, muna amfani da ainihin samfuri don ƙirƙirar kirtani wanda ya haɗa da name m.

 

A taƙaice, TypeScript yana da fitattun fasaloli kamar su duba nau'in a tsaye, mai tarawa da goyan bayan aiki da kai, duba kurakurai na lokaci-lokaci, module tallafin tsarin, da tsawaita daidaitawa da fasali. Waɗannan fasalulluka suna haɓaka dogaro, aiki, da sarrafa lamba yayin haɓaka aikace-aikacen.