Type Alias
Từ khóa type cho phép ta định nghĩa alias (tên bí danh) cho các kiểu sẵn có:
type alias = existingTypeKiểu existingType có thể là bất kỳ kiểu TS nào hợp lệ. Ví dụ:
type chars = string
let messsage: chars // same as string type
type alphanumeric = string | number
let input: alphanumeric
input = 100 // valid
input = "Hi" // valid
input = false // Compiler error
type Point = {
x: number
y: number
}
function printCoord(point: Point) {
console.log("The coordinate's x value is " + point.x)
console.log("The coordinate's y value is " + point.y)
}
printCoord({ x: 100, y: 100 })Chúng ta có thể extend một type thông qua toán tử & như sau:
type Info = {
name: string
}
type Student = Info & {
grade: number
}Interface Vs Type Alias
Interface và type alias rất giống nhau bởi vì chúng chỉ có một vài khác biệt nhỏ. Việc sử dụng cái nào là tùy thuộc vào lập trình viên.
Using with Primitive Types
Khác biệt đầu tiên giữa type alias (từ khóa type) và interface là type alias có thể được sử dụng để tạo alias cho các kiểu dữ liệu primitive1, union và tuple. Ví dụ:
// primitive
type Name = string
// object
type PartialPointX = { x: number }
type PartialPointY = { y: number }
// union
type PartialPoint = PartialPointX | PartialPointY
// tuple
type Data = [number, string]Extends
Khác biệt thứ hai là cả hai đều có thể extend, nhưng cú pháp của chúng khác nhau.
Interface extends interface:
interface PartialPointX {
x: number
}
interface Point extends PartialPointX {
y: number
}Interface extends type alias:
type PartialPointX = { x: number }
interface Point extends PartialPointX {
y: number
}Type alias extends type alias:
type PartialPointX = { x: number }
type Point = PartialPointX & { y: number }Type alias extends interface:
interface PartialPointX {
x: number
}
type Point = PartialPointX & { y: number }Implementation
Class có thể implement interface và type alias. Tuy nhiên, do interface (hay class) được xem như là các static blueprint nên chúng không thể implement một type alias của union type. Ví dụ:
interface Point {
x: number
y: number
}
class SomePoint implements Point {
x = 1
y = 2
}
type Point2 = {
x: number
y: number
}
class SomePoint2 implements Point2 {
x = 1
y = 2
}
type PartialPoint = { x: number } | { y: number }
// FIXME: can not implement a union type
class SomePartialPoint implements PartialPoint {
x = 1
y = 2
}Declaration Merging
Không giống như type alias, interface có thể được khai báo nhiều lần, tất cả các thuộc tính trong mỗi lần khai báo sẽ được hợp lại. Ví dụ:
// These two declarations become:
// interface Point { x: number y: number }
interface Point {
x: number
}
interface Point {
y: number
}
const point: Point = { x: 1, y: 2 }Summary
Sử dụng interface khi:
- Cần định nghĩa các object shape.
- Cần sự extending và implementing.
- Cần dùng declaration merging.
Sử dụng type alias khi:
- Sử dụng với các union type và các intersection.
- Cần kết hợp nhiều kiểu dữ liệu phức tạp.
- Cần một cái tên kiểu dữ liệu cô đọng và ngắn gọn.
Resources
- https://www.typescripttutorial.net/typescript-tutorial/typescript-type-aliases/
- https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-aliases
- https://stackoverflow.com/questions/37233735/interfaces-vs-types-in-typescript
Footnotes
-
Xem thêm JS Data Types ↩