TypeScript

Structural typing

Në shqip: Tipizimi strukturor

ShpjegimiSQ

TypeScript-i i krahason tipet sipas formës, jo sipas emrit: nëse një objekt ka të gjitha fushat që kërkohen, pranohet — edhe nëse është krijuar ndryshe. Ndryshe nga Java ose C#, ku emri i klasës ka rëndësi.

EnglishEN

TypeScript compares types by their shape, not their name: if an object has all the required fields, it's accepted — even if it was created differently. Unlike Java or C#, where the class name matters.

Si ta mendosh

„Nëse ecën si rosë dhe bën kuak si rosë, është rosë.“

Lexoje në anglisht

“If it walks like a duck and quacks like a duck, it's a duck.”

Shembull koditypescript

interface Pika { x: number; y: number }
const p = { x: 1, y: 2, ngjyra: "kuqe" };
function shfaq(p: Pika) {}
shfaq(p);   // në rregull — ka x dhe y

Terma të lidhur