type สองตัวเข้ากันได้ถ้า shape ตรงกัน ไม่สนใจชื่อหรือความสัมพันธ์ที่ประกาศไว้ นี่คือ model default ของ TypeScript ดู Structural Typing
Nominal typing
model ตรงข้าม (Java, C#): type สองตัวเข้ากันได้ก็ต่อเมื่อตัวหนึ่งถูกประกาศให้ extend หรือ implement อีกตัว TypeScript เลียนแบบ nominal typing ได้ด้วย private field กับ branded type แต่ default ยังเป็น structural ดู Structural Typing
Assignable / assignability
ค่าที่เป็น type A ใช้แทนที่ type B ได้หรือไม่ ตรวจด้วยการเทียบ shape (structural typing) ไม่ใช่เทียบชื่อ มองเป็นความสัมพันธ์แบบ subset ระหว่างเซตของ value ที่แต่ละ type อธิบาย ดู The Type-System Mindset
Subtype
A เป็น subtype ของ B เมื่อทุก value ของ A เป็น B ที่ใช้ได้ด้วย — เป็นความสัมพันธ์เดียวกับที่ assignability ตรวจ type ที่มี property จำเป็นมากกว่าเป็น subtype ของ type ที่มีน้อยกว่า (subset ของ value)
Widening
compiler ขยาย literal type ให้กลายเป็น base type ของมัน — "GET" กลายเป็น string — ตรงจุดที่ compiler สันนิษฐานว่า value อาจเปลี่ยนได้ในภายหลัง เห็นชัดที่สุดคือ let และ property ของ object ที่ไม่ได้ annotate ด้วย const ดู Literals & Widening
Narrowing
compiler ปรับ type ที่รู้จักของตัวแปรให้แคบลงภายใน branch หนึ่ง จากการเช็คก่อนหน้า (typeof, in, equality, custom type guard) ดู Narrowing
Literal type
type ที่มี value ได้ค่าเดียวเป๊ะ — "GET", 42, true — แทนที่จะเป็นเซตทั้งหมดของ string, number หรือ boolean ดู Literals & Widening
Union
A | B: value ที่เป็น หนึ่งใน หลาย type เซตของ value ที่ยอมรับคือ union (OR) ของเซตแต่ละตัว union จึง กว้างกว่า ส่วนประกอบของมัน ดู Unions & Intersections
Intersection
A & B: value ที่ตรงกับ ทุก type พร้อมกัน สำหรับ object type แปลว่ามีทุก property ของทุกตัว intersection จึง แคบกว่า ส่วนประกอบของมัน ดู Unions & Intersections
never
เซตว่าง — type ที่ไม่มี value ที่ใช้ได้เลย เป็น return type ของ function ที่ throw เสมอหรือวนไม่รู้จบ และเป็น type ที่เหลือใน branch ที่ถูก narrow จนไม่มีอะไรเหลือ ซึ่งเป็นกลไกที่ทำให้ exhaustiveness check ทำงานได้ ดู Nullability & never
unknown
คู่หูที่ type-safe ของ any: รับ value อะไรก็ได้ แต่ compiler บังคับให้ narrow ก่อนจะเอาไปทำอะไรต่อ ดู The Type-System Mindset
any
ทำให้ value หลุดออกจากการตรวจ type ไปเลย assign เข้าหรือออกได้กับทุกอย่างแบบเงียบ ๆ ทั้งสองทิศทาง — เป็น type เดียวที่ทำลาย soundness ของทุกอย่างที่เหลือ ดู Typing APIs & Boundaries
ชื่อสำหรับ type อะไรก็ได้ — object shape, union, tuple, mapped type หรือ conditional type เปิดเพิ่มทีหลังไม่ได้เมื่อประกาศแล้ว แต่แสดง shape ที่ interface ทำไม่ได้ ดู Objects, Arrays & Tuples
Generic
type, function หรือ class ที่ parameterize ด้วย type ที่ไม่ได้กำหนดตายตัวล่วงหน้า ทำให้ implementation เดียวใช้ได้กับหลาย type จริง โดยไม่เสีย type information ไปเป็น any ดู Generics
expression อะไรก็ตามที่ compiler รู้จักว่า narrow union ได้ — typeof x === "string", x instanceof Foo, "key" in x, หรือการเรียก custom function ที่มี return type เป็น type predicate ดู Narrowing
Type predicate
return type ของ function ในรูป x is T บอก compiler ว่าเมื่อ function คืน true แล้ว argument ของมันจะถูก narrow เป็น T — เป็นการอ้างที่ compiler เชื่อโดยไม่เช็ค body ของ function ว่าพิสูจน์จริงหรือเปล่า ดู Narrowing
Assertion function
function ที่ return type เป็น asserts x is T (หรือแค่ asserts x) narrow argument ของมันในทุกบรรทัด หลังจาก เรียกแล้ว return แบบปกติ แทนที่จะ narrow แค่ข้างใน branch if ดู Narrowing
Branded type
type ธรรมดา (ปกติเป็น string หรือ number) ที่ intersect กับ marker property เฉพาะที่จริง ๆ ไม่มีอยู่ตอน runtime ทำให้ compiler มองว่ามันแยกจาก value อื่นที่มี underlying type เดียวกัน เป็นเทคนิคมาตรฐานสำหรับ ID แบบ nominal ใน type system แบบ structural ดู Typing APIs & Boundaries
Discriminated union
union ของ object type ที่มี property ร่วมกันตัวหนึ่งเป็น literal type (เรียกว่า discriminant หรือ tag) ที่ value ของมันบอกได้ว่ากำลังถืออันไหนอยู่ ทำให้เช็คแค่ property นั้นตัวเดียวก็ narrow shape ที่เหลือได้ ดู Narrowing
Exhaustiveness
การเช็ค ปกติตอน compile time ว่าทุก member ของ union ถูกจัดการครบแล้ว วิธีทั่วไปคือ assign value ที่ “เป็นไปไม่ได้” ที่เหลือให้ตัวแปรที่ type เป็น never ใน branch default/else ซึ่งจะ compile ไม่ผ่านทันทีที่มี union member ใหม่ที่ยังไม่ถูกจัดการ ดู Narrowing
satisfies
operator ที่เช็ค expression กับ type โดยไม่เปลี่ยน inferred type ของ expression เองแบบที่ annotation หรือ as จะทำ — ได้ทั้งการ validate และ คง type ที่แคบและเป็น literal ไว้ ดู Literals & Widening
Function type เปล่า ๆ เป็น parameter หรือ return type
Function เข้าได้กับ value ที่เรียกได้ ทุกแบบ ไม่ว่า signature อะไร เรียกมันด้วย argument อะไรก็ type-check ผ่าน และ return type ของมันเป็น any มันดูเหมือนกำลัง type “a function” แต่จริง ๆ type “ไม่เช็คอะไรเลย” ตรงนั้นควรเป็น function signature จริง (หรือ generic constraint) แทน