Về mặt khái niệm, các loại liên kết và giao nhau có thể được coi là các toán tử logic logic [
5,ts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
6] khi chúng liên quan đến các loại. Hãy xem nhóm gồm hai bộ mục chồng chéo này làm ví dụts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
Try
0 thì nó sẽ bao gồm mọi mục trên toàn bộ biểu đồCác loại giao lộ cũng có tên và định nghĩa xuất phát từ lý thuyết tập hợp, nhưng chúng có thể được coi là AND, đối với các loại
Trong sơ đồ tương tự ở trên, nếu chúng ta muốn trái cây cũng chua [
1] thì cuối cùng chúng ta chỉ nhận đượcts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
2ts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
Các kiểu liên kết trong TypeScript
Các loại liên kết trong TypeScript có thể được mô tả bằng toán tử
3 [ống]ts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
Ví dụ: nếu chúng tôi có một loại có thể là một trong hai chuỗi,
4 hoặcts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
5, chúng tôi có thể định nghĩa nó làts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
ts
"success" | "error"
Ví dụ, hàm
6 sẽ trả vềts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
7 nếu một số được chọn từts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
8 là >= 0. 5, hoặcts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
9 nếu 0.5] return "heads"ts
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
Try
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
TryHãy làm cho điều này thú vị hơn một chút bằng cách sử dụng các bộ dữ liệu, được cấu trúc như sau
20 hoặc làts
Tryfunction flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
4 hoặc làts
Tryfunction flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
22ts
Tryfunction flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
23 một cái gì đó khác, tùy thuộc vào giá trị được tìm thấy trongts
Tryfunction flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
20ts
Tryfunction flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
- Trường hợp
4. một mẩu thông tin liên lạc.ts
Tryfunction flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
26ts
Tryfunction flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
- Trường hợp
5. một trường hợpts
Tryfunction flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
28ts
Tryfunction flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
- Trường hợp
Chúng tôi vẫn sẽ quyết định điều nào trong số những điều này thực sự xảy ra dựa trên việc tung đồng xu 50/50 của chúng tôi từ phía trên
2ts
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
Try
loại này thú vị hơn nhiều
Làm việc với các loại công đoàn
Hãy tiếp tục với ví dụ của chúng tôi ở trên và cố gắng làm điều gì đó với giá trị "kết quả"
Trước tiên, hãy hủy cấu trúc bộ dữ liệu và xem TypeScript nói gì về các thành viên của nó
2ts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
Một thời gian tốt để chọc xung quanh
Nhấp vào nút
29 và khám pháts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
20 vàts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
21 trong sân chơi TS. Khám phá những gì có sẵn trong tự động hoàn thành cho mỗits
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
6ts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
Chúng ta có thể thấy rằng thông tin tự động điền cho giá trị đầu tiên gợi ý rằng đó là một chuỗi. Điều này là do, bất kể đây là chuỗi cụ thể
4 hayts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
5, nó chắc chắn sẽ là một chuỗits
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
Giá trị thứ hai phức tạp hơn một chút — chỉ có thuộc tính
24 là có sẵn cho chúng tôi. Điều này là do, cả “đối tượng thông tin người dùng của chúng tôi và các phiên bản của lớpts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
28 đều có thuộc tínhts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
24 có giá trị là một chuỗits
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
Những gì chúng ta đang thấy ở đây là, khi một giá trị có loại bao gồm liên kết, chúng ta chỉ có thể sử dụng "hành vi chung" được đảm bảo ở đó
Thu hẹp với loại bảo vệ
Cuối cùng, chúng ta cần phải “tách biệt” hai khả năng tiềm ẩn cho giá trị của mình, nếu không chúng ta sẽ không thể tiến xa được. Chúng ta có thể làm điều này với các loại bảo vệ
Bộ bảo vệ loại là các biểu thức, khi được sử dụng với câu lệnh luồng điều khiển, cho phép chúng ta có một loại cụ thể hơn cho một giá trị cụ thể
Tôi thích coi đây là “chất keo” giữa kiểm tra kiểu thời gian biên dịch và thực thi thời gian chạy mã của bạn. Chúng tôi sẽ làm việc với một cái mà bạn đã quen thuộc để bắt đầu.
27ts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
3ts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
TypeScript có một sự hiểu biết đặc biệt về ý nghĩa của nó khi séc ________ 227 của bạn trả về ________ 229 hoặc ________ 260 và tạo ra một nhánh mã xử lý từng khả năng
Nó thậm chí còn tốt hơn…
Công đoàn bị phân biệt đối xử
7ts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
TypeScript hiểu rằng vị trí thứ nhất và thứ hai của bộ dữ liệu của chúng tôi được liên kết. Những gì chúng ta đang thấy ở đây đôi khi được gọi là loại công đoàn bị phân biệt đối xử hoặc “được gắn thẻ”
Các loại giao lộ trong TypeScript
Các loại giao lộ trong TypeScript có thể được mô tả bằng toán tử
61 [dấu và]ts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
Ví dụ: nếu chúng ta có một
62, có thêm các thuộc tínhts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
63 vàts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
64 thì sao?ts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
2ts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
Điều này hoàn toàn khác so với những gì chúng tôi đã thấy với các loại liên kết - đây thực sự là một
65 vàts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
66 được kết hợp với nhau và chúng tôi có quyền truy cập vào mọi thứ ngay lập tứcts
Try
function flipCoin[]: "heads" | "tails" {
if [Math.random[] > 0.5] return "heads"
return "tails"
}
const outcome = flipCoin[]
const outcome: "heads" | "tails"
Việc sử dụng các loại giao lộ ít phổ biến hơn nhiều so với các loại liên kết. Tôi hy vọng nó sẽ có tỷ lệ ít nhất là 50 trên 1 đối với bạn trong thực tế