Hướng dẫn how typescript disinfect html? - cách loại bỏ tập lệnh html?

Trang tổng quan này chứa một phiên bản rút ngắn của tất cả các ghi chú phát hành cho TypeScript. Bởi vì trang này rất lớn. Các mẫu mã có các yếu tố tương tác của chúng bị vô hiệu hóa.

Show

TypeScript 4.2

Bảo quản bí danh loại thông minh hơn

TypeScript có một cách để khai báo tên mới cho các loại gọi là bí danh loại. Nếu bạn viết một tập hợp các chức năng mà tất cả hoạt động trên

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

18, bạn có thể viết một bí danh để tránh lặp đi lặp lại nhiều lần.

ts

type BasicPrimitive = number | string | boolean;

Trong TypeScript 4.2, nội bộ của chúng tôi thông minh hơn một chút. Chúng tôi theo dõi cách các loại được xây dựng bằng cách giữ xung quanh các phần của cách chúng được viết và xây dựng ban đầu theo thời gian. Chúng tôi cũng theo dõi và phân biệt các bí danh loại vào các trường hợp của các bí danh khác!

Có thể in lại các loại dựa trên cách bạn đã sử dụng chúng trong mã của mình có nghĩa là với tư cách là người dùng TypeScript, bạn có thể tránh được một số loại không may được hiển thị và điều đó thường chuyển sang nhận ra đầu ra tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 tốt hơn, thông báo lỗi và thông báo lỗi và trong- Loại trình soạn thảo hiển thị trong thông tin nhanh và trợ giúp chữ ký. Điều này có thể giúp TypeScript cảm thấy dễ tiếp cận hơn một chút cho người mới đến.

Để biết thêm thông tin, hãy xem yêu cầu kéo đầu tiên cải thiện các trường hợp khác nhau xung quanh việc bảo tồn bí danh loại liên minh, cùng với yêu cầu kéo thứ hai bảo tồn bí danh gián tiếp.

Các yếu tố nghỉ ngơi hàng đầu/giữa trong các loại tuple

Trong TypeScript, các loại tuple có nghĩa là để mô hình các mảng với độ dài và loại phần tử cụ thể.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

Trong TypeScript 4.2, các phần tử REST được mở rộng cụ thể trong cách chúng có thể được sử dụng. Trong các phiên bản trước, TypeScript chỉ cho phép các phần tử

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

20 ở vị trí cuối cùng của loại tuple.

Tuy nhiên, bây giờ các yếu tố REST có thể xảy ra ở bất cứ đâu trong một tuple - chỉ với một vài hạn chế.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

Mặc dù JavaScript không có bất kỳ cú pháp nào để mô hình các tham số REST hàng đầu, chúng tôi vẫn có thể khai báo

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

21 là một hàm lấy các đối số hàng đầu bằng cách khai báo tham số REST

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

22 với loại Tuple sử dụng phần tử REST hàng đầu. Điều này có thể giúp mô hình rất nhiều JavaScript hiện có ngoài kia!

Để biết thêm chi tiết, xem yêu cầu kéo ban đầu.

Kiểm tra chặt chẽ hơn cho nhà điều hành tsinterface SomeType { /** This is an index signature. */ [propName: string]: any;}function doStuff(value: SomeType) { let x = value["someProperty"];}23

Trong JavaScript, đó là một lỗi thời gian chạy để sử dụng loại không phải đối tượng ở bên phải của toán tử

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

23. TypeScript 4.2 đảm bảo điều này có thể được bắt tại thời điểm thiết kế.

ts

// @errors: 2361

"foo" in 42;

Kiểm tra này khá bảo thủ đối với hầu hết các phần, vì vậy nếu bạn đã nhận được một lỗi về điều này, thì đó có thể là một vấn đề trong mã.

Một lời cảm ơn lớn đến người đóng góp bên ngoài của chúng tôi Jonas Hzigter vì yêu cầu kéo của họ!

tsinterface SomeType { /** This is an index signature. */ [propName: string]: any;}function doStuff(value: SomeType) { let x = value["someProperty"];}25

Quay lại khi TypeScript lần đầu tiên giới thiệu chữ ký chỉ mục, bạn chỉ có thể nhận được các thuộc tính được khai báo bởi chúng với cú pháp truy cập phần tử của phần tử có giá đỡ như

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

26.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

Điều này cuối cùng trở nên cồng kềnh trong các tình huống mà chúng ta cần làm việc với các đối tượng có tính chất tùy ý. Ví dụ, hãy tưởng tượng một API trong đó nó phổ biến để viết sai một tên thuộc tính bằng cách thêm một ký tự

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

27 ở cuối.

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

Để làm cho các loại tình huống này dễ dàng hơn, một thời gian trở lại, TypeScript có thể sử dụng cú pháp truy cập thuộc tính của Toted dotted, như

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

28 Khi một loại có chữ ký chỉ mục chuỗi. Điều này cũng giúp chuyển đổi mã JavaScript hiện tại sang TypeScript dễ dàng hơn.

Trong một số trường hợp, người dùng muốn chọn lọc một cách rõ ràng vào chữ ký chỉ mục - họ muốn nhận được thông báo lỗi khi truy cập thuộc tính chấm chấm không tương ứng với một khai báo thuộc tính cụ thể.

Đó là lý do tại sao TypeScript giới thiệu một lá cờ mới gọi là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

29. Trong chế độ này, bạn sẽ được chọn tham gia vào hành vi cũ của TypeScript, có lỗi. Cài đặt mới này không thuộc họ cờ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

30, vì chúng tôi tin rằng người dùng sẽ thấy nó hữu ích hơn trên một số bản mã nhất định so với các cơ sở mã khác.

Bạn có thể hiểu tính năng này chi tiết hơn bằng cách đọc theo yêu cầu kéo tương ứng. Chúng tôi cũng muốn gửi lời cảm ơn lớn đến Wenlu Wang, người đã gửi cho chúng tôi yêu cầu kéo này!

tsinterface SomeType { /** This is an index signature. */ [propName: string]: any;}function doStuff(value: SomeType) { let x = value["someProperty"];}31 Xây dựng chữ ký

TypeScript cho phép chúng tôi đánh dấu một lớp là trừu tượng. Điều này nói với TypeScript rằng lớp chỉ có nghĩa là được mở rộng từ đó và một số thành viên nhất định cần được điền bởi bất kỳ lớp con nào để thực sự tạo ra một thể hiện.

TypeScript 4.2 cho phép bạn chỉ định công cụ sửa đổi

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

31 trên chữ ký của hàm tạo.

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

Thêm công cụ sửa đổi

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

31 vào các tín hiệu chữ ký xây dựng mà bạn có thể truyền trong các hàm tạo

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

31. Nó không ngăn cản bạn vượt qua các chức năng khác của các lớp/nhà xây dựng, đó là bê tông, - nó thực sự chỉ báo hiệu rằng không có ý định chạy trực tiếp cho hàm tạo, do đó, nó an toàn để vượt qua trong một loại lớp.

Tính năng này cho phép chúng tôi viết các nhà máy mixin theo cách hỗ trợ các lớp trừu tượng. Ví dụ: trong đoạn mã sau, chúng tôi có thể sử dụng hàm mixin

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

35 với lớp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

31

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

37.

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

Lưu ý rằng

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

35 đang chứng minh một quy tắc cụ thể, trong đó một lớp (như

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

39) mở rộng một giá trị mà chung chung và giới hạn bởi một hàm tạo trừu tượng (như

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

40) cũng phải được khai báo

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

31. Điều này là do không có cách nào để biết liệu một lớp có thành viên trừu tượng hơn được thông qua, và vì vậy, không thể biết liệu lớp con có thực hiện tất cả các thành viên trừu tượng hay không.

Bạn có thể đọc thêm về chữ ký xây dựng trừu tượng theo yêu cầu kéo của nó.

Hiểu cấu trúc dự án của bạn với tsinterface SomeType { /** This is an index signature. */ [propName: string]: any;}function doStuff(value: SomeType) { let x = value["someProperty"];}42

Một kịch bản phổ biến đáng ngạc nhiên cho người dùng TypeScript là hỏi tại sao TypeScript bao gồm cả tệp này? Suy ra các tệp trong chương trình của bạn hóa ra là một quá trình phức tạp và do đó có rất nhiều lý do tại sao một sự kết hợp cụ thể của

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

43 đã được sử dụng, tại sao một số tệp trong

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

44 được đưa vào và tại sao một số tệp được đưa vào mặc dù chúng tôi nghĩ Chỉ định

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

45 sẽ giữ chúng ra.

Đó là lý do tại sao TypeScript bây giờ cung cấp cờ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

46.

sh

tsc --explainFiles

Khi sử dụng tùy chọn này, trình biên dịch TypeScript sẽ cung cấp một số đầu ra rất dài dòng về lý do tại sao một tệp kết thúc trong chương trình của bạn. Để đọc nó dễ dàng hơn, bạn có thể chuyển tiếp đầu ra đến một tệp hoặc gửi nó vào một chương trình có thể dễ dàng xem nó.

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

Thông thường, đầu ra sẽ bắt đầu bằng cách liệt kê các lý do bao gồm các tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

43, sau đó cho các tệp cục bộ, sau đó các tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

44.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

0

Ngay bây giờ, chúng tôi không đảm bảo về định dạng đầu ra - nó có thể thay đổi theo thời gian. Trên lưu ý đó, chúng tôi quan tâm đến việc cải thiện định dạng này nếu bạn có bất kỳ đề xuất nào!

Để biết thêm thông tin, hãy xem yêu cầu kéo ban đầu!

Cải thiện chức năng chưa được kiểm tra trong các biểu thức logic

Nhờ những cải tiến hơn nữa từ Alex Tarasyuk, các kiểm tra chức năng chưa được xác định của TypeScript hiện đang được áp dụng trong các biểu thức

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

49 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

50.

Theo

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

51, mã sau đây sẽ lỗi.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

1

Để biết thêm chi tiết, hãy xem yêu cầu kéo ở đây.

Các biến bị phá hủy có thể được đánh dấu rõ ràng là không được sử dụng

Nhờ một yêu cầu kéo khác từ Alex Tarasyuk, giờ đây bạn có thể đánh dấu các biến bị phá hủy là không sử dụng bằng tiền tố chúng bằng một dấu gạch dưới (ký tự

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

52).

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

2

Trước đây, nếu

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

53 không bao giờ được sử dụng sau này, TypeScript sẽ đưa ra lỗi theo

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

54. Bây giờ, TypeScript sẽ nhận ra rằng

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

53 đã được đặt tên một cách có chủ ý với một dấu gạch dưới vì không có ý định sử dụng nó.

Để biết thêm chi tiết, hãy xem sự thay đổi đầy đủ.

Các quy tắc thư giãn giữa các thuộc tính tùy chọn và chữ ký chỉ số chuỗi

Chữ ký chỉ mục chuỗi là một cách gõ các đối tượng giống như từ điển, nơi bạn muốn cho phép truy cập với các khóa tùy ý:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

3

Tất nhiên, đối với bất kỳ tiêu đề phim nào chưa có trong từ điển,

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

56 sẽ là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 (TypeScript 4.1 đã thêm tùy chọn

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

58 để bao gồm

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 khi đọc từ chữ ký chỉ mục như thế này). Mặc dù nó rõ ràng rằng phải có một số chuỗi không có trong

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

60, các phiên bản trước của các thuộc tính đối tượng tùy chọn được xử lý bằng TypeScript là không thể gán cho các chữ ký chỉ số tương thích khác, do sự hiện diện của

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

4

TypeScript 4.2 cho phép gán này. Tuy nhiên, nó không cho phép gán các thuộc tính không tùy chọn với

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 trong các loại của chúng, cũng không cho phép viết

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 vào một khóa cụ thể:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

5

Quy tắc mới cũng không áp dụng cho chữ ký chỉ số số, vì chúng được coi là giống như mảng và dày đặc:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

6

Bạn có thể hiểu rõ hơn về sự thay đổi này bằng cách đọc lên PR ban đầu.

Tuyên bố thiếu chức năng trợ giúp

Nhờ yêu cầu kéo theo cộng đồng từ Alexander Tarasyuk, giờ đây chúng tôi có một cách khắc phục nhanh chóng để tuyên bố các chức năng và phương thức mới dựa trên trang web cuộc gọi!

Hướng dẫn how typescript disinfect html? - cách loại bỏ tập lệnh html?

Phá vỡ thay đổi

Chúng tôi luôn cố gắng để giảm thiểu các thay đổi phá vỡ trong một bản phát hành. TypeScript 4.2 chứa một số thay đổi phá vỡ, nhưng chúng tôi tin rằng chúng nên được quản lý trong một bản nâng cấp.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

43 Cập nhật

Như với mọi phiên bản TypeScript, các khai báo cho

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

43 (đặc biệt là các khai báo được tạo cho bối cảnh web), đã thay đổi. Có nhiều thay đổi khác nhau, mặc dù

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

66 và ____ 467 có thể là người gây rối nhất.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

68 Lỗi áp dụng cho các biểu thức lỏng lẻo

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

69

Khi giá trị của biểu thức

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

69 được ghi lại, nhưng TypeScript có thể ngay lập tức tìm ra loại bạn dự định nhận được (tức là biểu thức

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

69 được nhập theo ngữ cảnh), giờ đây, TypeScript sẽ đưa ra lỗi

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

72.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

7

Xem thêm chi tiết trong các thay đổi tương ứng.

Kiểm tra chức năng chưa được mở rộng

Như đã mô tả ở trên, các kiểm tra chức năng chưa được đặt hiện tại sẽ hoạt động nhất quán trong các biểu thức

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

49 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

50 khi sử dụng

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

51. Đây có thể là một nguồn nghỉ mới, nhưng thường là dấu hiệu của lỗi logic trong mã hiện có.

Loại đối số trong JavaScript không được phân tích cú pháp là đối số loại

Các đối số loại đã không được phép trong JavaScript, nhưng trong TypeScript 4.2, trình phân tích cú pháp sẽ phân tích chúng theo cách tuân thủ đặc biệt hơn. Vì vậy, khi viết mã sau trong tệp JavaScript:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

8

TypeScript sẽ phân tích nó là JavaScript sau:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

9

Điều này có thể ảnh hưởng đến bạn nếu bạn đang tận dụng API TypeScript của các cấu trúc loại phân tích trong các tệp JavaScript, điều này có thể xảy ra khi cố gắng phân tích các tệp luồng.

Xem yêu cầu kéo để biết thêm chi tiết về những gì đã kiểm tra.

Giới hạn kích thước Tuple cho chênh lệch

Các loại tple có thể được thực hiện bằng cách sử dụng bất kỳ loại cú pháp lan truyền (

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

76) trong bản thảo.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

0

Đôi khi các loại tuple này có thể vô tình phát triển trở nên lớn, và điều đó có thể khiến việc kiểm tra loại mất nhiều thời gian. Thay vì để quá trình kiểm tra loại (đặc biệt xấu trong các kịch bản biên tập), TypeScript có một bộ giới hạn để tránh thực hiện tất cả các công việc đó.

Bạn có thể thấy yêu cầu kéo này để biết thêm chi tiết.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 Tiện ích mở rộng không thể được sử dụng trong các đường dẫn nhập

Trong TypeScript 4.2, giờ đây đây là một lỗi cho các đường dẫn nhập của bạn để chứa

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 trong phần mở rộng.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

1

Thay vào đó, đường dẫn nhập của bạn sẽ phản ánh bất cứ điều gì trình tải của bạn sẽ làm trong thời gian chạy. Bất kỳ nhập khẩu nào sau đây có thể có thể sử dụng thay thế.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

2

Hoàn nguyên mẫu suy luận theo nghĩa đen

Thay đổi này đã loại bỏ một tính năng từ Beta TypeScript 4.2. Nếu bạn đã không nâng cấp qua bản phát hành ổn định cuối cùng của chúng tôi, bạn đã thắng bị ảnh hưởng, nhưng bạn vẫn có thể quan tâm đến sự thay đổi.

Phiên bản Beta của TypeScript 4.2 bao gồm một thay đổi trong chuỗi Mẫu. Trong thay đổi này, các chuỗi chuỗi mẫu sẽ được cung cấp các loại chuỗi mẫu hoặc đơn giản hóa thành nhiều loại theo nghĩa đen. Các loại này sau đó sẽ mở rộng thành

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79 khi gán cho các biến có thể thay đổi.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

3

Điều này tương tự như cách thức hoạt động suy luận theo nghĩa đen của chuỗi.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

4

Vì lý do đó, chúng tôi tin rằng việc tạo các biểu thức chuỗi mẫu có các loại chuỗi mẫu sẽ là sự nhất quán của Cameron; Tuy nhiên, từ những gì chúng tôi đã thấy và nghe thấy, đó không phải là mong muốn.

Đáp lại, chúng tôi đã hoàn nguyên tính năng này (và thay đổi phá vỡ tiềm năng). Nếu bạn muốn một biểu thức chuỗi mẫu được cung cấp một loại giống như nghĩa đen, bạn luôn có thể thêm

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

80 vào cuối của nó.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

5

TypeScript 4.1

Mẫu các loại nghĩa đen

Chuỗi các loại chữ trong TypeScript cho phép chúng tôi mô hình hóa các chức năng và API mong đợi một tập hợp các chuỗi cụ thể.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

6

Điều này khá đẹp vì các loại theo nghĩa đen về cơ bản có thể kiểm tra chính tả các giá trị chuỗi của chúng tôi.

Chúng tôi cũng thích rằng các chữ viết có thể được sử dụng làm tên thuộc tính trong các loại được ánh xạ. Theo nghĩa này, họ cũng có thể sử dụng như các khối xây dựng:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

7

Nhưng có một nơi khác mà các loại chuỗi theo nghĩa đen có thể được sử dụng làm khối xây dựng: xây dựng các loại theo nghĩa đen khác.

Đó là lý do tại sao TypeScript 4.1 mang đến loại chuỗi theo nghĩa đen. Nó có cùng cú pháp với các chuỗi theo nghĩa đen trong JavaScript, nhưng được sử dụng ở các vị trí loại. Khi bạn sử dụng nó với các loại theo nghĩa đen cụ thể, nó sẽ tạo ra một loại chuỗi mới bằng cách kết hợp các nội dung.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

8

Điều gì xảy ra khi bạn có công đoàn trong các vị trí thay thế? Nó tạo ra tập hợp của mọi chuỗi có thể có thể được đại diện bởi mỗi thành viên công đoàn.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

9

Điều này có thể được sử dụng ngoài các ví dụ dễ thương trong ghi chú phát hành. Ví dụ, một số thư viện cho các thành phần UI có cách chỉ định cả căn chỉnh dọc và ngang trong API của chúng, thường với cả hai cùng một lúc sử dụng một chuỗi như

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

81. Giữa việc căn chỉnh theo chiều dọc với

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

82,

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

83 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

84 và căn chỉnh theo chiều ngang với

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

85,

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

86 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

87, có 9 chuỗi có thể trong đó mỗi chuỗi trước được kết nối với mỗi chuỗi sau sử dụng dấu gạch ngang.

ts

// @errors: 2361

"foo" in 42;

0

Mặc dù có rất nhiều ví dụ về loại API này trong tự nhiên, nhưng đây vẫn là một ví dụ đồ chơi vì chúng tôi có thể viết chúng ra bằng tay. Trong thực tế, đối với 9 chuỗi, điều này có thể tốt; Nhưng khi bạn cần một tấn chuỗi, bạn nên xem xét tự động tạo chúng trước thời hạn để lưu công việc trên mỗi loại kiểm tra (hoặc chỉ sử dụng

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79, sẽ đơn giản hơn nhiều để hiểu).lots of examples of this sort of API in the wild, this is still a bit of a toy example since we could write these out manually. In fact, for 9 strings, this is likely fine; but when you need a ton of strings, you should consider automatically generating them ahead of time to save work on every type-check (or just use

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79, which will be much simpler to comprehend).

Một số giá trị thực đến từ việc tạo ra các chữ cái mới. Ví dụ, hãy tưởng tượng API

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

89 lấy một đối tượng và tạo ra một đối tượng chủ yếu giống hệt nhau, nhưng với phương thức

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

90 mới để phát hiện các thay đổi đối với các thuộc tính.

ts

// @errors: 2361

"foo" in 42;

1

Lưu ý rằng

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

90 lắng nghe sự kiện

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

92, không chỉ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

93. Làm thế nào chúng ta sẽ gõ cái này?

ts

// @errors: 2361

"foo" in 42;

2

Với điều này, chúng ta có thể xây dựng một cái gì đó lỗi khi chúng ta đưa sai tài sản!

ts

// @errors: 2361

"foo" in 42;

3

Chúng ta cũng có thể làm một cái gì đó đặc biệt trong các loại theo nghĩa đen: chúng ta có thể suy ra từ các vị trí thay thế. Chúng ta có thể tạo ví dụ cuối cùng của chúng ta chung để suy ra từ các phần của chuỗi

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

94 để tìm ra thuộc tính liên quan.

ts

// @errors: 2361

"foo" in 42;

4

Ở đây chúng tôi đã biến

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

90 thành một phương pháp chung. Khi người dùng gọi với chuỗi

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

96, TypeScript sẽ cố gắng suy ra loại phù hợp với

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

97. Để làm điều đó, nó sẽ khớp với

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

97 so với nội dung trước

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

99 và suy ra chuỗi

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

93. Khi các bản thảo số lượng ra, phương thức

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

90 có thể tìm nạp loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

02 trên đối tượng gốc, đó là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79 trong trường hợp này. Tương tự, khi chúng tôi gọi với

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

04, nó tìm thấy loại cho thuộc tính

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

05 là

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

06).

Suy luận có thể được kết hợp theo những cách khác nhau, thường là để giải mã các chuỗi và tái cấu trúc chúng theo những cách khác nhau. Trên thực tế, để giúp sửa đổi các loại theo nghĩa đen này, chúng tôi đã thêm một vài bí danh loại tiện ích mới để sửa đổi vỏ trong các chữ cái (nghĩa là chuyển đổi sang các ký tự chữ thường và chữ hoa).

ts

// @errors: 2361

"foo" in 42;

5

Các bí danh loại mới là

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

07,

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

08,

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

09 và

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

10. Hai biến đổi đầu tiên mỗi ký tự trong một chuỗi và hai người sau chỉ biến đổi ký tự đầu tiên trong một chuỗi.

Để biết thêm chi tiết, hãy xem yêu cầu kéo ban đầu và yêu cầu kéo theo tiến trình để chuyển sang người trợ giúp loại bí danh.

Nhắc lại chính trong các loại được ánh xạ

Chỉ như một sự bồi dưỡng, một loại được ánh xạ có thể tạo các loại đối tượng mới dựa trên các khóa tùy ý

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

7

hoặc các loại đối tượng mới dựa trên các loại đối tượng khác.

ts

// @errors: 2361

"foo" in 42;

7

Cho đến bây giờ, các loại được ánh xạ chỉ có thể tạo ra các loại đối tượng mới với các khóa mà bạn đã cung cấp chúng; Tuy nhiên, rất nhiều thời gian bạn muốn có thể tạo các khóa mới hoặc lọc các khóa, dựa trên các đầu vào.

Đó là lý do tại sao TypeScript 4.1 cho phép bạn lập bản đồ lại các khóa theo các loại được ánh xạ với mệnh đề

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

11 mới.

ts

// @errors: 2361

"foo" in 42;

8

Với điều khoản

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

11 mới này, bạn có thể tận dụng các tính năng như các loại theo nghĩa đen để dễ dàng tạo tên thuộc tính dựa trên các tên cũ.

ts

// @errors: 2361

"foo" in 42;

9

Và bạn thậm chí có thể lọc các khóa bằng cách sản xuất

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13. Điều đó có nghĩa là bạn không phải sử dụng một loại trợ giúp

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

14 thêm trong một số trường hợp.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

0

Để biết thêm thông tin, hãy xem yêu cầu kéo ban đầu trên GitHub.

Các loại có điều kiện đệ quy

Trong JavaScript, nó khá phổ biến để thấy các chức năng có thể làm phẳng và xây dựng các loại container ở cấp độ tùy ý. Ví dụ, hãy xem xét phương pháp

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

15 trên các trường hợp

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

16.

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

17 đã mở ra mỗi lời hứa cho đến khi nó tìm thấy một giá trị mà không phải là lời hứa giống như lời hứa, và chuyển giá trị đó cho một cuộc gọi lại. Ngoài ra, còn có một phương pháp

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

18 tương đối mới trên

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

19 có thể có độ sâu của sâu để làm phẳng.

Thể hiện điều này trong hệ thống loại TypeScript, đối với tất cả các mục đích và mục đích thực tế, không thể. Mặc dù có những hack để đạt được điều này, các loại cuối cùng trông rất vô lý.

Đó là lý do tại sao TypeScript 4.1 làm giảm một số hạn chế đối với các loại có điều kiện - để chúng có thể mô hình hóa các mẫu này. Trong TypeScript 4.1, các loại có điều kiện hiện có thể ngay lập tức tham chiếu bản thân trong các nhánh của chúng, giúp việc viết các bí danh loại đệ quy dễ dàng hơn.

Ví dụ: nếu chúng tôi muốn viết một loại để lấy các loại phần tử của mảng lồng nhau, chúng tôi có thể viết loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

20 sau.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

1

Tương tự, trong TypeScript 4.1, chúng ta có thể viết một loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

21 thành Unwrap

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

16s sâu sắc.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

2

Hãy nhớ rằng trong khi các loại đệ quy này là mạnh mẽ, nhưng chúng nên được sử dụng có trách nhiệm và tiết kiệm.

Trước hết, các loại này có thể làm rất nhiều công việc, điều đó có nghĩa là chúng có thể tăng thời gian kiểm tra loại. Cố gắng mô hình hóa các số trong trình tự phỏng đoán Collatz hoặc Fibonacci có thể rất thú vị, nhưng don don gửi rằng trong các tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 trên NPM.

Nhưng ngoài việc tính toán chuyên sâu, các loại này có thể đạt giới hạn độ sâu đệ quy bên trong trên các đầu vào phức tạp đầy đủ. Khi giới hạn đệ quy đó bị ảnh hưởng, dẫn đến lỗi thời gian biên dịch. Nói chung, nó tốt hơn là không sử dụng các loại này hơn là viết một cái gì đó thất bại trên các ví dụ thực tế hơn.

Xem thêm tại triển khai.

Truy cập đã kiểm tra (tsinterface Options { /** File patterns to be excluded. */ exclude?: string[]; /** * It handles any extra properties that we haven't declared as type 'any'. */ [x: string]: any;}function processOptions(opts: Options) { // Notice we're *intentionally* accessing `excludes`, not `exclude` if (opts.excludes) { console.error( "The option `excludes` is not valid. Did you mean `exclude`?" ); }}24)

TypeScript có một tính năng gọi là chữ ký chỉ mục. Những chữ ký này là một cách để báo hiệu cho hệ thống loại mà người dùng có thể truy cập các thuộc tính được đặt tên tùy ý.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

3

Trong ví dụ trên,

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

25 có một chữ ký chỉ mục cho biết bất kỳ thuộc tính nào được truy cập mà không được liệt kê nên có loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

26. Điều này thường thuận tiện cho mã lạc quan giả định bạn biết những gì bạn đang làm, nhưng sự thật là hầu hết các giá trị trong JavaScript không hỗ trợ mọi tên tài sản tiềm năng. Ví dụ, hầu hết các loại sẽ không có giá trị cho khóa thuộc tính được tạo bởi

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

27 như trong ví dụ trước. Đối với nhiều người dùng, hành vi này là không mong muốn và cảm thấy như nó không tận dụng việc kiểm tra nghiêm ngặt đầy đủ của

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

51.

Đó là lý do tại sao TypeScript 4.1 vận chuyển với một lá cờ mới có tên là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

58. Trong chế độ mới này, mọi quyền truy cập thuộc tính (như

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

30) hoặc truy cập được lập chỉ mục (như

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

31) được coi là có khả năng không xác định. Điều đó có nghĩa là trong ví dụ cuối cùng của chúng tôi,

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

32 sẽ có loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

33 trái ngược với chỉ

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

26. Nếu bạn cần truy cập vào thuộc tính đó, bạn sẽ phải kiểm tra sự tồn tại của nó trước tiên hoặc sử dụng toán tử khẳng định không null (ký tự Postfix

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

35).

Cờ này có thể tiện dụng để bắt các lỗi ngoài giới hạn, nhưng nó có thể ồn ào đối với nhiều mã, do đó nó không được kích hoạt tự động bởi cờ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

30; Tuy nhiên, nếu tính năng này thú vị với bạn, bạn sẽ cảm thấy thoải mái khi thử nó và xác định xem liệu nó có hợp lý với cơ sở mã hóa nhóm của bạn không!

Bạn có thể tìm hiểu thêm tại yêu cầu kéo thực hiện.

tsinterface Options { /** File patterns to be excluded. */ exclude?: string[]; /** * It handles any extra properties that we haven't declared as type 'any'. */ [x: string]: any;}function processOptions(opts: Options) { // Notice we're *intentionally* accessing `excludes`, not `exclude` if (opts.excludes) { console.error( "The option `excludes` is not valid. Did you mean `exclude`?" ); }}37 mà không có tsinterface Options { /** File patterns to be excluded. */ exclude?: string[]; /** * It handles any extra properties that we haven't declared as type 'any'. */ [x: string]: any;}function processOptions(opts: Options) { // Notice we're *intentionally* accessing `excludes`, not `exclude` if (opts.excludes) { console.error( "The option `excludes` is not valid. Did you mean `exclude`?" ); }}38

Sử dụng ánh xạ đường dẫn là khá phổ biến - thường thì nó có thể nhập khẩu đẹp hơn, thường thì nó có thể mô phỏng hành vi liên kết monorepo.

Thật không may, việc chỉ định

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

37 để cho phép lập bản đồ đường dẫn cũng cần chỉ định một tùy chọn gọi là

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

38, cho phép đạt được các đường dẫn xác định trần so với

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

38. Điều này cũng thường khiến những con đường kém được sử dụng bởi các máy nhập tự động.

Trong TypeScript 4.1, tùy chọn

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

37 có thể được sử dụng mà không cần

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

38. Điều này giúp tránh một số vấn đề này.

tsinterface Options { /** File patterns to be excluded. */ exclude?: string[]; /** * It handles any extra properties that we haven't declared as type 'any'. */ [x: string]: any;}function processOptions(opts: Options) { // Notice we're *intentionally* accessing `excludes`, not `exclude` if (opts.excludes) { console.error( "The option `excludes` is not valid. Did you mean `exclude`?" ); }}44 ngụ ý tsinterface Options { /** File patterns to be excluded. */ exclude?: string[]; /** * It handles any extra properties that we haven't declared as type 'any'. */ [x: string]: any;}function processOptions(opts: Options) { // Notice we're *intentionally* accessing `excludes`, not `exclude` if (opts.excludes) { console.error( "The option `excludes` is not valid. Did you mean `exclude`?" ); }}45

Trước đây nếu bạn đang bắt đầu một dự án JavaScript đã kiểm tra, bạn phải đặt cả

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

45 và

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

44. Đây là một chút ma sát hơi khó chịu trong trải nghiệm, vì vậy

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

44 bây giờ ngụ ý

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

45 theo mặc định.

Xem thêm chi tiết theo yêu cầu kéo.

React 17 nhà máy JSX

TypeScript 4.1 hỗ trợ React 17, sắp tới

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

50 và

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

51 Các chức năng của nhà máy thông qua hai tùy chọn mới cho tùy chọn trình biên dịch

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

50:

  • ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    53
  • ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    54

Các tùy chọn này được dự định để sản xuất và phát triển biên dịch tương ứng. Thông thường, các tùy chọn từ người này có thể mở rộng từ cái kia.

Để biết thêm thông tin, hãy kiểm tra PR tương ứng.

TypeScript 4.0

Các loại tuple variadic

Hãy xem xét một chức năng trong JavaScript được gọi là

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

55 có hai loại mảng hoặc tuple và kết hợp chúng lại với nhau để tạo ra một mảng mới.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

4

Cũng xem xét

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

56, có một mảng hoặc tuple và trả về tất cả các yếu tố nhưng đầu tiên.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

5

Làm thế nào chúng ta sẽ gõ một trong hai trong số này trong TypeScript?

Đối với

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

55, điều hợp lệ duy nhất chúng tôi có thể làm trong các phiên bản cũ hơn của ngôn ngữ là thử và viết một số quá tải.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

6

Uh, không sao, đó là một lượng quá tải bảy khi mảng thứ hai luôn trống. Hãy để thêm một số khi

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

58 có một đối số.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

7

Chúng tôi hy vọng nó rõ ràng rằng điều này đang trở nên không hợp lý. Thật không may, bạn cũng kết thúc với các loại vấn đề tương tự khi gõ một chức năng như

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

56.

Đây là một trường hợp khác của những gì chúng tôi muốn gọi là cái chết của một ngàn người quá tải, và nó thậm chí không giải quyết vấn đề nói chung. Nó chỉ cung cấp các loại chính xác cho nhiều quá tải như chúng ta quan tâm để viết. Nếu chúng tôi muốn tạo ra một trường hợp bắt tất cả, chúng tôi cần một quá tải như sau:

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

8

Nhưng chữ ký đó không mã hóa bất cứ điều gì về độ dài của đầu vào hoặc thứ tự của các phần tử, khi sử dụng bộ dữ liệu.

TypeScript 4.0 mang đến hai thay đổi cơ bản, cùng với các cải tiến suy luận, để thực hiện việc gõ những thứ này có thể.

Thay đổi đầu tiên là sự lây lan trong cú pháp loại tuple giờ đây có thể là chung chung. Điều này có nghĩa là chúng ta có thể đại diện cho các hoạt động bậc cao trên các bộ dữ liệu và mảng ngay cả khi chúng ta không biết các loại thực tế mà chúng ta hoạt động. Khi sự lây lan chung được khởi tạo (hoặc, được thay thế bằng một loại thực) trong các loại tuple này, chúng có thể tạo ra các bộ mảng và tuple khác.

Ví dụ: điều đó có nghĩa là chúng ta có thể gõ chức năng như

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

56, mà không có cái chết của chúng ta bởi một ngàn sự cố quá tải.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

9

Thay đổi thứ hai là các yếu tố nghỉ ngơi có thể xảy ra ở bất cứ đâu trong một tuple - không chỉ ở cuối!

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

0

Trước đây, TypeScript sẽ đưa ra một lỗi như sau:

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

1

Nhưng với TypeScript 4.0, hạn chế này được thư giãn.

Lưu ý rằng trong các trường hợp khi chúng ta lan truyền theo một loại không có độ dài đã biết, loại kết quả cũng không bị ràng buộc và tất cả các yếu tố yếu tố sau đây vào loại phần tử REST kết quả.

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

2

Bằng cách kết hợp cả hai hành vi này với nhau, chúng ta có thể viết một chữ ký được gõ tốt cho

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

55:

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

3

Mặc dù một chữ ký đó vẫn còn hơi dài, nhưng nó chỉ là một chữ ký mà không phải lặp đi lặp lại, và nó đưa ra hành vi có thể dự đoán được trên tất cả các mảng và bộ dữ liệu.

Chức năng này là tuyệt vời, nhưng nó cũng tỏa sáng trong các kịch bản tinh vi hơn. Ví dụ, hãy xem xét một hàm để áp dụng một phần các đối số gọi là

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

62.

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

62 có chức năng - hãy để Lôi gọi nó

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

64 - cùng với một vài đối số ban đầu mà

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

64 mong đợi. Sau đó, nó trả về một chức năng mới có bất kỳ đối số nào khác mà

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

64 vẫn cần và gọi

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

64 khi nhận được chúng.

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

4

TypeScript 4.0 cải thiện quy trình suy luận cho các tham số REST và các yếu tố phần còn lại để chúng ta có thể gõ cái này và có nó chỉ hoạt động.

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

5

Trong trường hợp này,

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

62 hiểu được thông số nào có thể và ban đầu có thể thực hiện và trả về các chức năng chấp nhận và từ chối một cách thích hợp bất cứ điều gì còn sót lại.

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

6

Các loại tuple variadic cho phép rất nhiều mẫu thú vị mới, đặc biệt là xung quanh thành phần chức năng. Chúng tôi hy vọng chúng tôi có thể tận dụng nó để thực hiện một phương pháp JavaScript kiểm tra công việc tốt hơn. Một số ít các cải tiến và mô hình suy luận khác cũng đã đi vào điều này, và nếu bạn quan tâm đến việc tìm hiểu thêm, bạn có thể xem yêu cầu kéo cho các bộ dữ liệu khác nhau.

Nhãn các yếu tố tuple

Cải thiện trải nghiệm xung quanh các loại Tuple và danh sách tham số rất quan trọng vì nó cho phép chúng tôi nhận được xác thực được gõ mạnh mẽ xung quanh các thành ngữ JavaScript thông thường - thực sự chỉ cần cắt và cắt giảm danh sách đối số và chuyển chúng cho các chức năng khác. Ý tưởng rằng chúng ta có thể sử dụng các loại Tuple cho các tham số REST là một nơi mà điều này rất quan trọng.

Ví dụ: chức năng sau sử dụng loại tuple làm tham số REST

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

7

Không nên xuất hiện không khác với chức năng sau đây

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

8

Đối với bất kỳ người gọi nào của

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

70.

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

9

Có một nơi mà sự khác biệt bắt đầu trở nên có thể quan sát được: khả năng đọc. Trong ví dụ đầu tiên, chúng tôi không có tên tham số cho các phần tử thứ nhất và thứ hai. Mặc dù những điều này không có tác động đến việc kiểm tra loại, việc thiếu nhãn trên các vị trí tuple có thể khiến chúng khó sử dụng hơn - khó khăn hơn để truyền đạt ý định của chúng tôi.

Đó là lý do tại sao trong TypeScript 4.0, các loại Tuples hiện có thể cung cấp nhãn.

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

0

Để làm sâu sắc kết nối giữa các danh sách tham số và loại tuple, cú pháp cho các phần tử REST và các phần tử tùy chọn phản ánh cú pháp cho danh sách tham số.

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

1

Có một vài quy tắc khi sử dụng các bộ dữ liệu được dán nhãn. Đối với một, khi dán nhãn một phần tử tuple, tất cả các phần tử khác trong tuple cũng phải được dán nhãn.

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

2

Nó đáng chú ý - các nhãn don don yêu cầu chúng tôi đặt tên cho các biến của chúng tôi khác nhau khi phá hủy. Họ hoàn toàn ở đó để tài liệu và công cụ.

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

3

Nhìn chung, các bộ dữ liệu được dán nhãn rất tiện dụng khi tận dụng các mẫu xung quanh các bộ dữ liệu và danh sách đối số, cùng với việc thực hiện quá tải theo cách an toàn kiểu. Trên thực tế, hỗ trợ Trình chỉnh sửa TypeScript, sẽ cố gắng hiển thị chúng dưới dạng quá tải khi có thể.

Hướng dẫn how typescript disinfect html? - cách loại bỏ tập lệnh html?

Để tìm hiểu thêm, hãy xem yêu cầu kéo cho các yếu tố tuple được dán nhãn.

Suy luận thuộc tính lớp từ các nhà xây dựng

TypeScript 4.0 hiện có thể sử dụng phân tích luồng điều khiển để xác định các loại thuộc tính trong các lớp khi

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

68 được bật.

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

4

Trong trường hợp không phải tất cả các đường dẫn của một hàm tạo gán cho một thành viên thể hiện, tài sản được coi là có khả năng là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57.

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

5

Trong trường hợp bạn biết rõ hơn (ví dụ: bạn có phương thức

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

73 thuộc loại nào đó), bạn sẽ vẫn cần một chú thích loại rõ ràng cùng với xác nhận gán xác định (

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

35) nếu bạn ở trong

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

75.

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

6

Để biết thêm chi tiết, xem yêu cầu kéo thực hiện.

Các toán tử phân công ngắn mạch

JavaScript và rất nhiều ngôn ngữ khác, hỗ trợ một bộ toán tử được gọi là toán tử gán hợp chất. Các toán tử gán hợp chất áp dụng một toán tử cho hai đối số, và sau đó gán kết quả cho bên trái. Bạn có thể đã thấy những điều này trước đây:

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

7

Vì vậy, nhiều toán tử trong JavaScript có một toán tử gán tương ứng! Tuy nhiên, cho đến gần đây, có ba trường hợp ngoại lệ đáng chú ý: logic và (

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

49), logic hoặc (

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

50) và kết hợp vô hiệu hóa (

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

78).

Đó là lý do tại sao TypeScript 4.0 hỗ trợ tính năng Ecmascript mới để thêm ba toán tử gán mới:

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

79,

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

80 và

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

81.

Các toán tử này rất tốt để thay thế bất kỳ ví dụ nào trong đó người dùng có thể viết mã như sau:

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

8

Hoặc một khối

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

82 tương tự như

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

9

Thậm chí còn có một số mẫu mà chúng tôi đã thấy (hoặc, uh, tự viết) để khởi tạo một cách uể oải, chỉ khi chúng sẽ cần thiết.

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

0

(Hãy nhìn xem, chúng tôi không tự hào về tất cả các mã chúng tôi viết)

Trong trường hợp hiếm hoi mà bạn sử dụng getters hoặc setters có tác dụng phụ, điều đáng chú ý là các nhà khai thác này chỉ thực hiện các bài tập nếu cần thiết. Theo nghĩa đó, không chỉ là mặt bên phải của người vận hành.

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

1

Hãy thử chạy ví dụ sau để xem điều đó khác với việc luôn thực hiện bài tập như thế nào.

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

2

Chúng tôi muốn gửi lời cảm ơn lớn đến thành viên cộng đồng Wenlu Wang vì đóng góp này!

Để biết thêm chi tiết, bạn có thể xem yêu cầu kéo ở đây. Bạn cũng có thể kiểm tra kho lưu trữ đề xuất của TC39 cho tính năng này.

tsinterface Options { /** File patterns to be excluded. */ exclude?: string[]; /** * It handles any extra properties that we haven't declared as type 'any'. */ [x: string]: any;}function processOptions(opts: Options) { // Notice we're *intentionally* accessing `excludes`, not `exclude` if (opts.excludes) { console.error( "The option `excludes` is not valid. Did you mean `exclude`?" ); }}83 trên tsinterface Options { /** File patterns to be excluded. */ exclude?: string[]; /** * It handles any extra properties that we haven't declared as type 'any'. */ [x: string]: any;}function processOptions(opts: Options) { // Notice we're *intentionally* accessing `excludes`, not `exclude` if (opts.excludes) { console.error( "The option `excludes` is not valid. Did you mean `exclude`?" ); }}84 BINDING điều khoản

Kể từ những ngày đầu của TypeScript, các biến mệnh đề

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

84 luôn được gõ là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

72. Điều này có nghĩa là TypeScript cho phép bạn làm bất cứ điều gì bạn muốn với họ.

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

3

Trên đây có một số hành vi không mong muốn nếu chúng tôi cố gắng ngăn chặn nhiều lỗi xảy ra trong mã xử lý lỗi của chúng tôi! Bởi vì các biến này có loại

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

72 theo mặc định, chúng không có bất kỳ an toàn loại nào có thể bị lỗi trên các hoạt động không hợp lệ.

Đó là lý do tại sao TypeScript 4.0 bây giờ cho phép bạn chỉ định loại biến mệnh đề

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

84 là

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

83 thay thế.

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

83 an toàn hơn

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

72 vì nó nhắc nhở chúng ta rằng chúng ta cần thực hiện một số loại kiểm tra loại trước khi hoạt động trên các giá trị của chúng ta.

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

4

Mặc dù các loại biến

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

84 đã giành được sự thay đổi theo mặc định, chúng tôi có thể xem xét cờ chế độ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

30 mới trong tương lai để người dùng có thể chọn tham gia hành vi này. Trong khi đó, có thể viết một quy tắc xơ để buộc các biến

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

84 để có một chú thích rõ ràng của

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

95 hoặc

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

96.

Để biết thêm chi tiết, bạn có thể xem xét các thay đổi cho tính năng này.

Nhà máy JSX tùy chỉnh

Khi sử dụng JSX, một đoạn là một loại phần tử JSX cho phép chúng ta trả về nhiều phần tử con. Khi chúng tôi lần đầu tiên thực hiện các mảnh vỡ trong TypeScript, chúng tôi đã có một ý tưởng tuyệt vời về cách các thư viện khác sẽ sử dụng chúng. Ngày nay, hầu hết các thư viện khác khuyến khích sử dụng JSX và các mảnh hỗ trợ có hình API tương tự.

Trong TypeScript 4.0, người dùng có thể tùy chỉnh nhà máy Fragment thông qua tùy chọn

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

97 mới.

Ví dụ, tệp

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98 sau đây cho TypeScript chuyển đổi JSX theo cách tương thích với React, nhưng chuyển mỗi lần gọi nhà máy thành

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

99 thay vì

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

00 và sử dụng

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

01 thay vì

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

02.

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

5

Trong trường hợp bạn cần có một nhà máy JSX khác nhau trên cơ sở mỗi tệp, bạn có thể tận dụng bình luận

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

03 Pragma mới. Ví dụ, các phần sau đây

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

6

Sầu sẽ được chuyển đổi sang đầu ra này JavaScript

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

7

Chúng tôi muốn gửi lời cảm ơn lớn đến thành viên cộng đồng NOJ VEK vì đã gửi yêu cầu kéo này và làm việc kiên nhẫn với nhóm của chúng tôi về nó.

Bạn có thể thấy rằng yêu cầu kéo để biết thêm chi tiết!

TypeScript 3.9

Những cải tiến trong suy luận và tsabstract class Shape { abstract getArea(): number;}// ---cut---interface HasArea { getArea(): number;}// Works!let Ctor: abstract new () => HasArea = Shape;04

Các phiên bản gần đây của TypeScript (khoảng 3.7) đã có các bản cập nhật cho các khai báo của các hàm như

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

04 và

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

06. Thật không may, điều đó đã giới thiệu một vài hồi quy, đặc biệt là khi trộn các giá trị với

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57.

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

8

Đây là hành vi kỳ lạ! Thực tế là

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

09 có chứa

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 bằng cách nào đó đã bị nhiễm độc

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

11 để bao gồm

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57.

Nhờ yêu cầu kéo từ Jack Bates, điều này đã được sửa chữa với các cải tiến trong quá trình suy luận của chúng tôi trong TypeScript 3.9. Ở trên không còn lỗi nữa. Nếu bạn đã bị mắc kẹt trên các phiên bản cũ hơn của TypeScript do các vấn đề vào khoảng

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

16, chúng tôi khuyến khích bạn cho 3,9 lần bắn!

Còn loại tsabstract class Shape { abstract getArea(): number;}// ---cut---interface HasArea { getArea(): number;}// Works!let Ctor: abstract new () => HasArea = Shape;14 thì sao?

Nếu bạn đã theo dõi các ghi chú cuộc họp thiết kế và theo dõi vấn đề của chúng tôi, bạn có thể biết về một số công việc xung quanh một nhà điều hành loại mới có tên

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

14. Mục tiêu này của toán tử loại này là mô hình chính xác cách

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

16 unwrapping hoạt động trong JavaScript.

Ban đầu, chúng tôi dự đoán vận chuyển

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

14 trong TypeScript 3.9, nhưng khi chúng tôi chạy các bản dựng bản tính ban đầu với các cơ sở mã hiện có, chúng tôi đã nhận ra rằng tính năng này cần nhiều công việc thiết kế hơn trước khi chúng tôi có thể đưa nó ra cho mọi người một cách trơn tru. Do đó, chúng tôi đã quyết định rút tính năng ra khỏi chi nhánh chính của chúng tôi cho đến khi chúng tôi cảm thấy tự tin hơn. Chúng tôi sẽ thử nghiệm nhiều hơn với tính năng này, nhưng chúng tôi đã giành chiến thắng khi vận chuyển nó như một phần của bản phát hành này.

Cải tiến tốc độ

TypeScript 3.9 tàu với nhiều cải tiến tốc độ mới. Nhóm của chúng tôi đã tập trung vào hiệu suất sau khi quan sát tốc độ biên dịch/chỉnh sửa cực kỳ kém với các gói như vật liệu-UI và thành phần kiểu dáng. Chúng tôi đã lặn sâu ở đây, với một loạt các yêu cầu kéo khác nhau nhằm tối ưu hóa một số trường hợp bệnh lý nhất định liên quan đến các công đoàn lớn, giao điểm, loại có điều kiện và các loại được ánh xạ.

  • https://github.com/microsoft/TypeScript/pull/36576
  • https://github.com/microsoft/TypeScript/pull/36590
  • https://github.com/microsoft/TypeScript/pull/36607
  • https://github.com/microsoft/TypeScript/pull/36622
  • https://github.com/microsoft/TypeScript/pull/36754
  • https://github.com/microsoft/TypeScript/pull/36696

Mỗi trong số các yêu cầu kéo này tăng khoảng 5-10% thời gian biên dịch trên các cơ sở mã nhất định. Tổng cộng, chúng tôi tin rằng chúng tôi đã đạt được khoảng 40% thời gian biên dịch vật liệu-UI!

Chúng tôi cũng có một số thay đổi để sắp xếp lại chức năng tệp trong các kịch bản của trình soạn thảo. Chúng tôi đã nghe từ nhóm mã Visual Studio rằng khi đổi tên một tệp, chỉ cần tìm ra câu lệnh nhập nào cần được cập nhật có thể mất từ ​​5 đến 10 giây. TypeScript 3.9 giải quyết vấn đề này bằng cách thay đổi phần bên trong của cách Tra cứu tệp trình biên dịch và dịch vụ ngôn ngữ.

Mặc dù vẫn còn chỗ để cải thiện, chúng tôi hy vọng công việc này sẽ chuyển sang trải nghiệm khéo léo hơn cho mọi người!

Hãy tưởng tượng rằng chúng tôi đang viết một thư viện trong TypeScript và chúng tôi đang xuất một số chức năng gọi là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

21 như một phần của API công khai của chúng tôi. Các loại chức năng của các loại tuyên bố rằng phải mất hai

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79 để người dùng TypeScript khác có thể gặp lỗi kiểm tra loại, nhưng cũng có kiểm tra thời gian chạy (có thể chỉ trong các bản dựng phát triển) để cho người dùng JavaScript một lỗi hữu ích.

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

9

Vì vậy, người dùng TypeScript sẽ nhận được một Squiggle màu đỏ hữu ích và thông báo lỗi khi họ sử dụng sai chức năng này và người dùng JavaScript sẽ gặp lỗi xác nhận. Chúng tôi muốn kiểm tra hành vi này, vì vậy chúng tôi sẽ viết một bài kiểm tra đơn vị.

sh

tsc --explainFiles

0

Thật không may nếu các bài kiểm tra của chúng tôi được viết bằng TypeScript, TypeScript sẽ cho chúng tôi một lỗi!

sh

tsc --explainFiles

1

Đó là lý do tại sao TypeScript 3.9 mang đến một tính năng mới:

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

20 Nhận xét. Khi một dòng được đặt trước với một bình luận

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

20, TypeScript sẽ triệt tiêu lỗi đó khỏi được báo cáo; Nhưng nếu không có lỗi, TypeScript sẽ báo cáo rằng

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

20 không cần thiết.

Ví dụ nhanh chóng, mã sau là ổn

sh

tsc --explainFiles

2

Trong khi mã sau

sh

tsc --explainFiles

3

kết quả trong lỗi

sh

tsc --explainFiles

4

Chúng tôi muốn gửi lời cảm ơn lớn đến Josh Goldberg, người đóng góp đã thực hiện tính năng này. Để biết thêm thông tin, bạn có thể xem yêu cầu kéo

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

23.

tsabstract class Shape { abstract getArea(): number;}// ---cut---interface HasArea { getArea(): number;}// Works!let Ctor: abstract new () => HasArea = Shape;24 hoặc tsabstract class Shape { abstract getArea(): number;}// ---cut---interface HasArea { getArea(): number;}// Works!let Ctor: abstract new () => HasArea = Shape;23?

Trong một số cách

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

20 có thể hoạt động như một nhận xét đàn áp, tương tự như

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

27. Sự khác biệt là

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

27 sẽ không làm gì nếu dòng sau không có lỗi.

Bạn có thể bị cám dỗ chuyển các bình luận

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

27 hiện có sang

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

20 và bạn có thể tự hỏi cái nào phù hợp với mã trong tương lai. Mặc dù nó hoàn toàn phụ thuộc vào bạn và nhóm của bạn, chúng tôi có một số ý tưởng để chọn trong một số tình huống nhất định.

Chọn

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

23 nếu:

  • Bạn viết mã kiểm tra trong đó bạn thực sự muốn hệ thống loại bị lỗi khi hoạt động
  • Bạn mong đợi một sửa chữa sẽ đến khá nhanh và bạn chỉ cần một cách giải quyết nhanh chóng
  • Bạn có thể tham gia một dự án có quy mô hợp lý với một nhóm chủ động muốn xóa các nhận xét đàn áp vì mã bị ảnh hưởng sớm là hợp lệ trở lại

Chọn

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

24 nếu:

  • Bạn có một dự án lớn hơn và các lỗi mới đã xuất hiện trong mã không có chủ sở hữu rõ ràng
  • Bạn đang ở giữa một bản nâng cấp giữa hai phiên bản khác nhau của TypeScript và một dòng lỗi trong một phiên bản nhưng không phải là phiên bản khác.
  • Bạn thành thật don don có thời gian để quyết định tùy chọn nào trong số này tốt hơn.

Kiểm tra chức năng chưa được đặt trong các biểu thức có điều kiện

Trong TypeScript 3.7, chúng tôi đã giới thiệu các kiểm tra chức năng chưa được gọi để báo cáo lỗi khi bạn quên gọi một hàm.

sh

tsc --explainFiles

5

Tuy nhiên, lỗi này chỉ áp dụng cho các điều kiện trong các câu lệnh

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

82. Nhờ yêu cầu kéo từ Alexander Tarasyuk, tính năng này hiện cũng được hỗ trợ trong các điều kiện ternary (nghĩa là cú pháp

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

34).

sh

tsc --explainFiles

6

https://github.com/microsoft/TypeScript/issues/36048

Cải tiến biên tập

Trình biên dịch TypeScript không chỉ cung cấp năng lượng cho trải nghiệm chỉnh sửa TypeScript trong hầu hết các biên tập viên chính, nó còn cung cấp năng lượng cho trải nghiệm JavaScript trong gia đình biên tập viên Visual Studio và hơn thế nữa. Sử dụng chức năng TypeScript/JavaScript mới trong trình soạn thảo của bạn sẽ khác nhau tùy thuộc vào trình soạn thảo của bạn, nhưng

  • Visual Studio Code hỗ trợ chọn các phiên bản khác nhau của TypeScript. Ngoài ra, có phần mở rộng JavaScript/TypeScript hàng đêm để ở trên cạnh chảy máu (thường rất ổn định).
  • Visual Studio 2017/2019 có [Trình cài đặt SDK ở trên] và cài đặt MSBuild.
  • Text Sublime 3 hỗ trợ chọn các phiên bản khác nhau của TypeScript

CommonJS Auto-Alports trong JavaScript

Một cải tiến mới tuyệt vời là trong các tệp tự động trong các tệp JavaScript bằng các mô-đun CommonJS.

Trong các phiên bản cũ hơn, TypeScript luôn cho rằng bất kể tệp của bạn là gì, bạn muốn nhập kiểu Ecmascript như

sh

tsc --explainFiles

7

Tuy nhiên, không phải ai cũng nhắm mục tiêu các mô-đun kiểu Ecmascript khi viết các tệp JavaScript. Rất nhiều người dùng vẫn sử dụng nhập khẩu theo phong cách CommonJS

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

35 như vậy

sh

tsc --explainFiles

8

TypeScript bây giờ tự động phát hiện các loại nhập khẩu mà bạn sử dụng để giữ cho tập tin của bạn sạch sẽ và nhất quán.

Để biết thêm chi tiết về thay đổi, hãy xem yêu cầu kéo tương ứng.

Hành động mã bảo tồn dòng mới

Các bản tái cấu trúc và sửa chữa nhanh chóng thường không làm một công việc tuyệt vời để bảo tồn các dòng mới. Như một ví dụ thực sự cơ bản, lấy mã sau.

sh

tsc --explainFiles

9

Nếu chúng tôi nhấn mạnh phạm vi từ

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

36 đến

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

37 trong trình chỉnh sửa của chúng tôi để trích xuất thành một hàm mới, chúng tôi sẽ kết thúc với mã như sau.

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

0

Hướng dẫn how typescript disinfect html? - cách loại bỏ tập lệnh html?

Đó không phải là lý tưởng - chúng tôi đã có một ranh giới trống giữa mỗi câu trong vòng

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

38 của chúng tôi, nhưng việc tái cấu trúc đã loại bỏ nó! TypeScript 3.9 làm thêm một chút để bảo tồn những gì chúng ta viết.

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

0

Hướng dẫn how typescript disinfect html? - cách loại bỏ tập lệnh html?

Bạn có thể xem thêm về việc thực hiện trong yêu cầu kéo này

Sửa lỗi nhanh để thiếu biểu thức trả về

Có những lúc chúng ta có thể quên trả về giá trị của câu lệnh cuối cùng trong một hàm, đặc biệt là khi thêm niềng răng xoăn vào các hàm mũi tên.

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

2

Nhờ yêu cầu kéo từ thành viên cộng đồng Wenlu Wang, TypeScript có thể cung cấp bản sửa lỗi nhanh để thêm các câu lệnh

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

39 bị thiếu, xóa niềng răng xoăn hoặc thêm dấu ngoặc đơn vào các cơ quan chức năng mũi tên trông đáng ngờ giống như chữ cái.

Hướng dẫn how typescript disinfect html? - cách loại bỏ tập lệnh html?

Hỗ trợ cho các tập tin của Phong cách giải pháp ”tsinterface Options { /** File patterns to be excluded. */ exclude?: string[]; /** * It handles any extra properties that we haven't declared as type 'any'. */ [x: string]: any;}function processOptions(opts: Options) { // Notice we're *intentionally* accessing `excludes`, not `exclude` if (opts.excludes) { console.error( "The option `excludes` is not valid. Did you mean `exclude`?" ); }}98

Các biên tập viên cần tìm ra tệp cấu hình nào mà một tệp thuộc về để nó có thể áp dụng các tùy chọn phù hợp và tìm ra các tệp khác nào được đưa vào dự án hiện tại. Theo mặc định, các trình chỉnh sửa được cung cấp bởi máy chủ ngôn ngữ TypeScript, thực hiện điều này bằng cách đi lên từng thư mục cha để tìm một

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98.

Một trường hợp mà điều này hơi rơi là khi

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98 chỉ đơn giản tồn tại để tham khảo các tệp

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98 khác.

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

3

Tệp này thực sự không làm gì khác ngoài việc quản lý các tệp dự án khác thường được gọi là giải pháp trên mạng trong một số môi trường. Ở đây, không có tệp

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

44 nào được máy chủ chọn, nhưng chúng tôi thực sự thích máy chủ ngôn ngữ để hiểu rằng tệp

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

45 hiện tại có thể thuộc về một trong những dự án được đề cập trong root

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98 này.

TypeScript 3.9 thêm hỗ trợ để chỉnh sửa các kịch bản cho cấu hình này. Để biết thêm chi tiết, hãy xem yêu cầu kéo thêm chức năng này.

Phá vỡ thay đổi

Phân tích sự khác biệt trong chuỗi tùy chọn và xác nhận không null

TypeScript gần đây đã triển khai toán tử chuỗi tùy chọn, nhưng chúng tôi đã nhận được phản hồi của người dùng rằng hành vi của chuỗi tùy chọn (

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

47) với toán tử khẳng định không null (

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

35) là cực kỳ phản trực giác.

Cụ thể, trong các phiên bản trước, mã

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

4

được giải thích là tương đương với JavaScript sau.

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

5

Trong mã trên, các dấu ngoặc đơn dừng hành vi ngắn mạch của người Viking về chuỗi tùy chọn, vì vậy nếu

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

70 là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57, truy cập

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

51 sẽ gây ra lỗi thời gian chạy.

Nhóm Babel đã chỉ ra hành vi này và hầu hết người dùng cung cấp phản hồi cho chúng tôi, tin rằng hành vi này là sai. Chúng tôi cũng vậy! Điều chúng tôi nghe thấy nhiều nhất là nhà điều hành

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

35 chỉ nên biến mất vì mục đích là loại bỏ

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 khỏi loại

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

55.

Nói cách khác, hầu hết mọi người đều cảm thấy rằng đoạn trích ban đầu nên được hiểu là

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

6

trong đó chỉ đánh giá thành

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 khi

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

70 là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57.

Đây là một sự thay đổi phá vỡ, nhưng chúng tôi tin rằng hầu hết các mã đã được viết với cách giải thích mới trong tâm trí. Người dùng muốn trở lại hành vi cũ có thể thêm các dấu ngoặc đơn rõ ràng xung quanh phía bên trái của toán tử

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

35.

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

4

tsabstract class Shape { abstract getArea(): number;}// ---cut---interface HasArea { getArea(): number;}// Works!let Ctor: abstract new () => HasArea = Shape;60 và tsabstract class Shape { abstract getArea(): number;}// ---cut---interface HasArea { getArea(): number;}// Works!let Ctor: abstract new () => HasArea = Shape;61 hiện là các ký tự văn bản JSX không hợp lệ

Thông số kỹ thuật JSX cấm sử dụng các ký tự

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

60 và

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

61 trong các vị trí văn bản. Cả TypeScript và Babel đều quyết định thực thi quy tắc này để trở nên hài hước hơn. Cách mới để chèn các ký tự này là sử dụng mã thoát HTML (ví dụ:

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

64) hoặc chèn biểu thức bằng một chuỗi chữ (ví dụ:

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

65).

May mắn thay, nhờ yêu cầu kéo thực thi điều này từ Brad Zacher, bạn sẽ nhận được một thông báo lỗi dọc theo các dòng

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

8

Ví dụ:

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

9

Thông báo lỗi đó đi kèm với một bản sửa lỗi nhanh chóng, và nhờ Alexander Tarasyuk, bạn có thể áp dụng những thay đổi này với số lượng lớn nếu bạn có nhiều lỗi.

Kiểm tra chặt chẽ hơn trên các giao lộ và các thuộc tính tùy chọn

Nói chung, một loại giao điểm như

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

66 có thể gán cho

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

67 nếu

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

68 hoặc

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

69 có thể được gán cho

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

67; Tuy nhiên, đôi khi điều đó có vấn đề với các thuộc tính tùy chọn. Ví dụ, hãy lấy những điều sau:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

00

Trong các phiên bản trước của TypeScript, điều này đã được cho phép vì trong khi

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

68 hoàn toàn không tương thích với

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

67,

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

69 tương thích với

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

67.

Trong TypeScript 3.9, miễn là mọi loại trong giao lộ là loại đối tượng cụ thể, hệ thống loại sẽ xem xét tất cả các thuộc tính cùng một lúc. Do đó, TypeScript sẽ thấy rằng thuộc tính

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

75 của

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

66 không tương thích với

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

67:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

01

Để biết thêm thông tin về thay đổi này, hãy xem yêu cầu kéo tương ứng.

Giao điểm giảm bởi các thuộc tính phân biệt

Có một vài trường hợp bạn có thể kết thúc với các loại mô tả các giá trị chỉ tồn tại. Ví dụ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

02

Mã này hơi kỳ lạ bởi vì thực sự không có cách nào để tạo ra giao điểm của

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

78 và

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

79 - chúng có hai trường

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

80 không tương thích. Trong các phiên bản trước của TypeScript, mã này đã được cho phép và loại

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

80 là

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13 vì

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

83 đã mô tả một tập hợp các giá trị có thể tồn tại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13.

Trong TypeScript 3.9, hệ thống loại tích cực hơn ở đây - thông báo rằng nó không thể giao nhau

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

78 và

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

79 vì các thuộc tính

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

80 của chúng. Vì vậy, thay vì thu gọn loại

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

88 xuống

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13, nó làm sụp đổ loại

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

90 (

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

91) xuống

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13. Điều đó có nghĩa là mã trên bây giờ lỗi với:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

03

Hầu hết các lần nghỉ chúng tôi quan sát thấy dường như tương ứng với các khai báo loại hơi không chính xác. Để biết thêm chi tiết, xem yêu cầu kéo ban đầu.

Trong các phiên bản cũ hơn của TypeScript,

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

93 và

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

94 người truy cập trong các lớp học được phát ra theo cách khiến chúng có thể gây ra được; Tuy nhiên, điều này không tuân thủ đặc điểm kỹ thuật của Ecmascript trong đó nói rằng chúng phải không thể kích thích. Do đó, mã TypeScript nhắm mục tiêu ES5 và ES2015 có thể khác nhau về hành vi.

Nhờ yêu cầu kéo từ người dùng GitHub Pathurs, TypeScript 3.9 hiện phù hợp hơn với Ecmascript về vấn đề này.

Loại tham số mở rộng tsinterface SomeType { /** This is an index signature. */ [propName: string]: any;}function doStuff(value: SomeType) { let x = value["someProperty"];}72 không còn hoạt động như tsinterface SomeType { /** This is an index signature. */ [propName: string]: any;}function doStuff(value: SomeType) { let x = value["someProperty"];}72

Trong các phiên bản trước của TypeScript, một tham số loại bị ràng buộc với

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

72 có thể được coi là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

72.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

04

Đây là một sự giám sát, vì vậy TypeScript 3.9 có một cách tiếp cận bảo thủ hơn và đưa ra một lỗi đối với các hoạt động đáng ngờ này.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

05

tsabstract class Shape { abstract getArea(): number;}// ---cut---interface HasArea { getArea(): number;}// Works!let Ctor: abstract new () => HasArea = Shape;99 luôn được giữ lại

Trong các phiên bản TypeScript trước đây, các khai báo như

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

00 sẽ bị loại bỏ trong đầu ra JavaScript của chúng tôi nếu

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

70 didn xuất khẩu bất kỳ giá trị nào. Loại phát ra này có vấn đề vì nó được định hướng loại và có thể được mô phỏng bởi Babel. TypeScript 3.9 sẽ luôn phát ra các khai báo

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

99 này. Trong thực tế, chúng tôi không mong đợi điều này sẽ phá vỡ nhiều mã hiện có.

TypeScript 3.8

Nhập khẩu và xuất khẩu chỉ loại

Tính năng này là điều mà hầu hết người dùng có thể không bao giờ phải suy nghĩ; Tuy nhiên, nếu bạn đã gặp phải các vấn đề theo

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

03, API hoặc BABEL của TypeScript ____704, tính năng này có thể có liên quan.

TypeScript 3.8 thêm một cú pháp mới cho nhập và xuất khẩu chỉ loại.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

06

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

05 Chỉ nhập các khai báo được sử dụng cho các chú thích và khai báo loại. Nó luôn bị xóa hoàn toàn, vì vậy, không có tàn dư của nó trong thời gian chạy. Tương tự,

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

06 chỉ cung cấp một xuất khẩu có thể được sử dụng cho bối cảnh loại và cũng bị xóa khỏi đầu ra TypeScript.

Điều quan trọng cần lưu ý là các lớp có giá trị trong thời gian chạy và loại vào thời gian thiết kế và việc sử dụng có tính nhạy cảm với ngữ cảnh. Khi sử dụng

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

05 để nhập một lớp, bạn có thể làm những việc như mở rộng từ nó.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

07

Nếu bạn đã sử dụng dòng chảy trước đó, cú pháp khá giống nhau. Một điểm khác biệt là chúng tôi đã thêm một vài hạn chế để tránh mã có vẻ mơ hồ.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

08

Kết hợp với

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

05, TypeScript 3.8 cũng thêm một cờ trình biên dịch mới để kiểm soát những gì xảy ra với nhập khẩu won won được sử dụng trong thời gian chạy:

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

09. Cờ này có 3 giá trị khác nhau:

  • ts

    abstract class SuperClass {

    abstract someMethod(): void;

    badda() {}

    }

    type AbstractConstructor<T> = abstract new (...args: any[]) => T

    function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

    abstract class StyledClass extends Ctor {

    getStyles() {

    // ...

    }

    }

    return StyledClass;

    }

    class SubClass extends withStyles(SuperClass) {

    someMethod() {

    this.someMethod()

    }

    }

    10: Đây là hành vi ngày nay của việc bỏ các khoản nhập khẩu này. Nó sẽ tiếp tục là mặc định và là một thay đổi không phá vỡ.
  • ts

    abstract class SuperClass {

    abstract someMethod(): void;

    badda() {}

    }

    type AbstractConstructor<T> = abstract new (...args: any[]) => T

    function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

    abstract class StyledClass extends Ctor {

    getStyles() {

    // ...

    }

    }

    return StyledClass;

    }

    class SubClass extends withStyles(SuperClass) {

    someMethod() {

    this.someMethod()

    }

    }

    11: Điều này bảo tồn tất cả các nhập khẩu có giá trị không bao giờ được sử dụng. Điều này có thể khiến nhập khẩu/tác dụng phụ được bảo tồn.
  • ts

    abstract class SuperClass {

    abstract someMethod(): void;

    badda() {}

    }

    type AbstractConstructor<T> = abstract new (...args: any[]) => T

    function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

    abstract class StyledClass extends Ctor {

    getStyles() {

    // ...

    }

    }

    return StyledClass;

    }

    class SubClass extends withStyles(SuperClass) {

    someMethod() {

    this.someMethod()

    }

    }

    12: Điều này bảo tồn tất cả các lần nhập (giống như tùy chọn

    ts

    abstract class SuperClass {

    abstract someMethod(): void;

    badda() {}

    }

    type AbstractConstructor<T> = abstract new (...args: any[]) => T

    function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

    abstract class StyledClass extends Ctor {

    getStyles() {

    // ...

    }

    }

    return StyledClass;

    }

    class SubClass extends withStyles(SuperClass) {

    someMethod() {

    this.someMethod()

    }

    }

    11), nhưng sẽ lỗi khi nhập giá trị chỉ được sử dụng dưới dạng loại. Điều này có thể hữu ích nếu bạn muốn đảm bảo không có giá trị nào được vô tình nhập, nhưng vẫn làm cho nhập khẩu hiệu ứng phụ rõ ràng.

Để biết thêm thông tin về tính năng này, bạn có thể xem yêu cầu kéo và các thay đổi có liên quan xung quanh việc mở rộng nơi nhập khẩu từ khai báo

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

05 có thể được sử dụng.

Trường riêng ECMAScript

TypeScript 3.8 mang lại sự hỗ trợ cho các trường riêng của Ecmascript, một phần của đề xuất trường Lớp Giai đoạn 3.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

09

Không giống như các thuộc tính thông thường (ngay cả các thuộc tính được khai báo với công cụ sửa đổi

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

15), các trường riêng có một vài quy tắc cần lưu ý. Một số trong số họ là:

  • Các trường riêng bắt đầu với một ký tự

    ts

    abstract class SuperClass {

    abstract someMethod(): void;

    badda() {}

    }

    type AbstractConstructor<T> = abstract new (...args: any[]) => T

    function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

    abstract class StyledClass extends Ctor {

    getStyles() {

    // ...

    }

    }

    return StyledClass;

    }

    class SubClass extends withStyles(SuperClass) {

    someMethod() {

    this.someMethod()

    }

    }

    16. Đôi khi chúng tôi gọi những tên riêng này.
  • Mỗi tên trường riêng được phân chia duy nhất đến lớp chứa của nó.
  • Trình sửa đổi khả năng truy cập TypeScript như

    ts

    abstract class SuperClass {

    abstract someMethod(): void;

    badda() {}

    }

    type AbstractConstructor<T> = abstract new (...args: any[]) => T

    function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

    abstract class StyledClass extends Ctor {

    getStyles() {

    // ...

    }

    }

    return StyledClass;

    }

    class SubClass extends withStyles(SuperClass) {

    someMethod() {

    this.someMethod()

    }

    }

    17 hoặc

    ts

    abstract class SuperClass {

    abstract someMethod(): void;

    badda() {}

    }

    type AbstractConstructor<T> = abstract new (...args: any[]) => T

    function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

    abstract class StyledClass extends Ctor {

    getStyles() {

    // ...

    }

    }

    return StyledClass;

    }

    class SubClass extends withStyles(SuperClass) {

    someMethod() {

    this.someMethod()

    }

    }

    15 có thể được sử dụng trên các trường riêng.
  • Các trường riêng có thể được truy cập hoặc thậm chí được phát hiện bên ngoài lớp chứa - ngay cả bởi người dùng JS! Đôi khi chúng ta gọi đây là sự riêng tư khó khăn.

Ngoài quyền riêng tư của người Viking, một lợi ích khác của các lĩnh vực tư nhân là sự độc đáo mà chúng tôi vừa đề cập. Ví dụ, khai báo tài sản thường xuyên dễ bị ghi đè trong các lớp con.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

10

Với các trường riêng, bạn sẽ không bao giờ phải lo lắng về điều này, vì mỗi tên trường là duy nhất cho lớp chứa.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

11

Một điều đáng chú ý khác là việc truy cập vào một trường tư nhân trên bất kỳ loại nào khác sẽ dẫn đến

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

19!

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

12

Cuối cùng, đối với bất kỳ người dùng tệp

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

20 nào, các trường riêng luôn phải được khai báo trước khi họ được gán cho.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

13

JavaScript luôn cho phép người dùng truy cập các thuộc tính không được khai báo, trong khi TypeScript luôn yêu cầu khai báo cho các thuộc tính lớp. Với các trường riêng, các khai báo luôn cần thiết bất kể chúng tôi làm việc trong các tệp

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

20 hay

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

45.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

14

Để biết thêm thông tin về việc triển khai, bạn có thể xem yêu cầu kéo ban đầu

Tôi nên sử dụng cái nào?

Chúng tôi đã nhận được nhiều câu hỏi về loại tư nhân nào bạn nên sử dụng làm người dùng TypeScript: Thông thường nhất, tôi có nên sử dụng từ khóa

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

15 hoặc các trường riêng của Ecmascript, Hash/pound (

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

16) không? Nó phụ thuộc!

Khi nói đến các thuộc tính, các công cụ sửa đổi TypeScript ____715 đã bị xóa hoàn toàn - điều đó có nghĩa là trong thời gian chạy, nó hoạt động hoàn toàn giống như một tài sản bình thường và không có cách nào để nói rằng nó được tuyên bố với một công cụ sửa đổi

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

15. Khi sử dụng từ khóa

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

15, quyền riêng tư chỉ được thực thi vào thời gian biên dịch/thời gian thiết kế và đối với người tiêu dùng JavaScript, nó hoàn toàn dựa trên ý định.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

15

Ưu điểm là loại quyền riêng tư mềm này có thể giúp người tiêu dùng của bạn tạm thời làm việc xung quanh việc không có quyền truy cập vào một số API và cũng hoạt động trong bất kỳ thời gian chạy nào.

Mặt khác, các tư nhân Ecmascript từ

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

16 hoàn toàn không thể truy cập được bên ngoài lớp.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

16

Quyền riêng tư khó khăn này thực sự hữu ích để đảm bảo nghiêm ngặt rằng không ai có thể sử dụng bất kỳ nội bộ nào của bạn. Nếu bạn là một tác giả thư viện, việc loại bỏ hoặc đổi tên một trường riêng sẽ không bao giờ gây ra sự thay đổi phá vỡ.

Như chúng tôi đã đề cập, một lợi ích khác là phân lớp con có thể dễ dàng hơn với các tư nhân Ecmascript ____ ____716 vì chúng thực sự là riêng tư. Khi sử dụng Ecmascript

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

16 Các trường riêng, không có lớp con nào phải lo lắng về các vụ va chạm trong việc đặt tên hiện trường. Khi nói đến các khai báo tài sản của TypeScript ____ ____715, người dùng vẫn phải cẩn thận không chà đạp lên các thuộc tính được khai báo trong các siêu lớp.

Một điều nữa để suy nghĩ là nơi bạn dự định cho mã của mình để chạy. TypeScript hiện có thể hỗ trợ tính năng này trừ khi nhắm mục tiêu các mục tiêu Ecmascript 2015 (ES6) hoặc cao hơn. Điều này là do việc triển khai hạ thấp của chúng tôi sử dụng

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

32s để thực thi quyền riêng tư và

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

32 có thể được ghi nhận theo cách không gây rò rỉ bộ nhớ. Ngược lại, các thuộc tính được khai báo của TypeScript ____ 715 hoạt động với tất cả các mục tiêu - thậm chí ECMAScript 3!

Việc xem xét cuối cùng có thể là tốc độ: Các thuộc tính

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

15 không khác với bất kỳ tài sản nào khác, vì vậy việc truy cập chúng nhanh như bất kỳ truy cập tài sản nào khác bất kể thời gian chạy mà bạn nhắm mục tiêu. Ngược lại, vì các trường riêng

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

16 được hạ thấp bằng cách sử dụng

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

32s, chúng có thể chậm hơn khi sử dụng. Mặc dù một số thời gian có thể tối ưu hóa việc triển khai thực tế của họ trên các trường riêng ____716 và thậm chí có các triển khai

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

32 nhanh chóng, đó có thể không xảy ra trong tất cả các thời gian.

tsabstract class SuperClass { abstract someMethod(): void; badda() {}}type AbstractConstructor = abstract new (...args: any[]) => Tfunction withStyles>(Ctor: T) { abstract class StyledClass extends Ctor { getStyles() { // ... } } return StyledClass;}class SubClass extends withStyles(SuperClass) { someMethod() { this.someMethod() }}40 Cú pháp

Nó thường phổ biến để có một điểm nhập cảnh duy nhất phơi bày tất cả các thành viên của một mô-đun khác như một thành viên duy nhất.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

17

Điều này rất phổ biến đến nỗi Ecmascript 2020 gần đây đã thêm một cú pháp mới để hỗ trợ mẫu này!

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

18

Đây là một cải tiến chất lượng tốt đẹp cho JavaScript và TypeScript 3.8 thực hiện cú pháp này. Khi mục tiêu mô -đun của bạn sớm hơn

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

41, TypeScript sẽ xuất một cái gì đó dọc theo các dòng của đoạn mã đầu tiên.

Cấp cao nhất tsabstract class SuperClass { abstract someMethod(): void; badda() {}}type AbstractConstructor = abstract new (...args: any[]) => Tfunction withStyles>(Ctor: T) { abstract class StyledClass extends Ctor { getStyles() { // ... } } return StyledClass;}class SubClass extends withStyles(SuperClass) { someMethod() { this.someMethod() }}42

TypeScript 3.8 cung cấp hỗ trợ cho tính năng Ecmascript sắp tới có tên là cấp cao nhất

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

42.

Người dùng JavaScript thường giới thiệu chức năng

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

44 để sử dụng

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

42, và sau đó gọi ngay vào chức năng sau khi xác định nó.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

19

Điều này là do trước đây trong JavaScript (cùng với hầu hết các ngôn ngữ khác có tính năng tương tự),

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

42 chỉ được phép trong phần thân của hàm

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

44. Tuy nhiên, với cấp cao nhất

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

42, chúng ta có thể sử dụng

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

42 ở cấp cao nhất của một mô-đun.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

20

Lưu ý có một sự tinh tế: cấp cao nhất

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

42 chỉ hoạt động ở cấp cao nhất của mô-đun và các tệp chỉ được coi là mô-đun khi TypeScript tìm thấy

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

51 hoặc

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

52. Trong một số trường hợp cơ bản, bạn có thể cần phải viết ra

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

53 dưới dạng một số nồi hơi để đảm bảo điều này.

Cấp cao nhất

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

42 có thể không hoạt động trong tất cả các môi trường mà bạn có thể mong đợi tại thời điểm này. Hiện tại, bạn chỉ có thể sử dụng cấp cao nhất

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

42 khi tùy chọn trình biên dịch

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

56 là

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

57 trở lên và

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

58 là

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

59 hoặc

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

60. Hỗ trợ trong một số môi trường và các gói có thể bị hạn chế hoặc có thể yêu cầu hỗ trợ thử nghiệm.

Để biết thêm thông tin về việc triển khai của chúng tôi, bạn có thể kiểm tra yêu cầu kéo ban đầu.

tsabstract class SuperClass { abstract someMethod(): void; badda() {}}type AbstractConstructor = abstract new (...args: any[]) => Tfunction withStyles>(Ctor: T) { abstract class StyledClass extends Ctor { getStyles() { // ... } } return StyledClass;}class SubClass extends withStyles(SuperClass) { someMethod() { this.someMethod() }}41 cho tsabstract class SuperClass { abstract someMethod(): void; badda() {}}type AbstractConstructor = abstract new (...args: any[]) => Tfunction withStyles>(Ctor: T) { abstract class StyledClass extends Ctor { getStyles() { // ... } } return StyledClass;}class SubClass extends withStyles(SuperClass) { someMethod() { this.someMethod() }}56 và tsabstract class SuperClass { abstract someMethod(): void; badda() {}}type AbstractConstructor = abstract new (...args: any[]) => Tfunction withStyles>(Ctor: T) { abstract class StyledClass extends Ctor { getStyles() { // ... } } return StyledClass;}class SubClass extends withStyles(SuperClass) { someMethod() { this.someMethod() }}58

TypeScript 3.8 hỗ trợ

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

41 như một tùy chọn cho

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

58 và

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

56. Điều này sẽ bảo tồn các tính năng ECMAScript 2020 mới hơn như chuỗi tùy chọn, Nullish Coalescing,

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

40 và cú pháp

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

68 động. Nó cũng có nghĩa là

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

69 nghĩa đen hiện có

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

56 ổn định dưới

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

59.

Công cụ sửa đổi tài sản JSDOC

TypeScript 3.8 hỗ trợ các tệp JavaScript bằng cách bật cờ

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

45 và cũng hỗ trợ kiểm tra loại các tệp JavaScript đó thông qua tùy chọn

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

44 hoặc bằng cách thêm nhận xét

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

74 vào đầu các tệp

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

20 của bạn.

Bởi vì các tệp JavaScript không có cú pháp chuyên dụng để kiểm tra loại, TypeScript tận dụng JSDOC. TypeScript 3.8 hiểu một vài thẻ JSDOC mới cho các thuộc tính.

Đầu tiên là các sửa đổi khả năng truy cập:

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

76,

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

77 và

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

78. Các thẻ này hoạt động chính xác như

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

17,

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

15 và

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

81 tương ứng hoạt động trong TypeScript.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

21
  • ts

    abstract class SuperClass {

    abstract someMethod(): void;

    badda() {}

    }

    type AbstractConstructor<T> = abstract new (...args: any[]) => T

    function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

    abstract class StyledClass extends Ctor {

    getStyles() {

    // ...

    }

    }

    return StyledClass;

    }

    class SubClass extends withStyles(SuperClass) {

    someMethod() {

    this.someMethod()

    }

    }

    76 luôn được ngụ ý và có thể bị bỏ lại, nhưng có nghĩa là một tài sản có thể đạt được từ bất cứ đâu.
  • ts

    abstract class SuperClass {

    abstract someMethod(): void;

    badda() {}

    }

    type AbstractConstructor<T> = abstract new (...args: any[]) => T

    function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

    abstract class StyledClass extends Ctor {

    getStyles() {

    // ...

    }

    }

    return StyledClass;

    }

    class SubClass extends withStyles(SuperClass) {

    someMethod() {

    this.someMethod()

    }

    }

    77 có nghĩa là một thuộc tính chỉ có thể được sử dụng trong lớp chứa.
  • ts

    abstract class SuperClass {

    abstract someMethod(): void;

    badda() {}

    }

    type AbstractConstructor<T> = abstract new (...args: any[]) => T

    function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

    abstract class StyledClass extends Ctor {

    getStyles() {

    // ...

    }

    }

    return StyledClass;

    }

    class SubClass extends withStyles(SuperClass) {

    someMethod() {

    this.someMethod()

    }

    }

    78 có nghĩa là một thuộc tính chỉ có thể được sử dụng trong lớp chứa và tất cả các lớp con có nguồn gốc, nhưng không phải trên các trường hợp không giống nhau của lớp chứa.

Tiếp theo, chúng tôi cũng đã thêm công cụ sửa đổi

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

85 để đảm bảo rằng một tài sản chỉ được viết vào trong khi khởi tạo.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

22

Thư mục tốt hơn xem trên Linux và tsabstract class SuperClass { abstract someMethod(): void; badda() {}}type AbstractConstructor = abstract new (...args: any[]) => Tfunction withStyles>(Ctor: T) { abstract class StyledClass extends Ctor { getStyles() { // ... } } return StyledClass;}class SubClass extends withStyles(SuperClass) { someMethod() { this.someMethod() }}86

TypeScript 3.8 vận chuyển một chiến lược mới để xem các thư mục, điều này rất quan trọng để chọn hiệu quả các thay đổi thành

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

44.

Đối với một số bối cảnh, trên các hệ điều hành như Linux, TypeScript cài đặt các trình theo dõi thư mục (trái ngược với người theo dõi tệp) trên

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

44 và nhiều thư mục con của nó để phát hiện các thay đổi phụ thuộc. Điều này là do số lượng người theo dõi tệp có sẵn thường bị lu mờ bởi các tệp trong

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

44, trong khi đó có ít thư mục hơn để theo dõi.

Các phiên bản cũ hơn của TypeScript sẽ ngay lập tức cài đặt các trình xem thư mục trên các thư mục và khi khởi động, điều đó sẽ ổn; Tuy nhiên, trong quá trình cài đặt NPM, rất nhiều hoạt động sẽ diễn ra trong

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

44 và điều đó có thể áp đảo các bản thảo, thường làm chậm các phiên biên tập viên để thu thập thông tin. Để ngăn chặn điều này, TypeScript 3.8 chờ đợi một chút trước khi cài đặt các trình theo dõi thư mục để cung cấp cho các thư mục có độ biến động cao này một thời gian để ổn định.

Bởi vì mọi dự án có thể hoạt động tốt hơn theo các chiến lược khác nhau và cách tiếp cận mới này có thể không hoạt động tốt cho quy trình công việc của bạn, TypeScript 3.8 giới thiệu một trường

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

86 mới trong

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98 và

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

93 cho phép người dùng nói với dịch vụ trình biên dịch/ngôn ngữ mà các chiến lược sẽ được sử dụng Theo dõi các tập tin và thư mục.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

23

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

86 chứa 4 tùy chọn mới có thể được cấu hình để xử lý cách TypeScript theo dõi các thay đổi.

Để biết thêm thông tin về những thay đổi này, hãy đến GitHub để xem yêu cầu kéo để đọc thêm.

“Kiểm tra gia tăng nhanh chóng và lỏng lẻo

TypeScript 3.8 Giới thiệu tùy chọn trình biên dịch mới có tên là

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

95. Khi tùy chọn này được bật, TypeScript sẽ tránh kiểm tra/xây dựng lại tất cả các tệp thực sự có thể bị ảnh hưởng và chỉ kiểm tra lại/xây dựng lại các tệp đã thay đổi cũng như các tệp trực tiếp nhập chúng.

Trong một cơ sở mã như Visual Studio Code, điều này đã giảm thời gian xây dựng lại cho các thay đổi trong một số tệp nhất định từ khoảng 14 giây xuống còn khoảng 1 giây. Mặc dù chúng tôi không nhất thiết phải đề xuất tùy chọn này cho tất cả các cơ sở mã, bạn có thể quan tâm nếu bạn có một cơ sở mã cực lớn và sẵn sàng trì hoãn các lỗi dự án đầy đủ cho đến sau này (ví dụ: bản dựng chuyên dụng thông qua

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

96 hoặc trong CI).

Để biết thêm chi tiết, bạn có thể thấy yêu cầu kéo ban đầu.

TypeScript 3.7

Chuỗi tùy chọn

Sân chơi

Chuỗi tùy chọn là vấn đề #16 trên Trình theo dõi vấn đề của chúng tôi. Đối với bối cảnh, đã có hơn 23.000 vấn đề về trình theo dõi vấn đề TypeScript kể từ đó.

Ở cốt lõi của nó, chuỗi tùy chọn cho phép chúng tôi viết mã trong đó TypeScript có thể ngừng chạy một số biểu thức nếu chúng tôi chạy vào

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57. Ngôi sao của chương trình trong chuỗi tùy chọn là toán tử

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

47 mới để truy cập thuộc tính tùy chọn. Khi chúng tôi viết mã như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

24

Đây là một cách để nói rằng khi

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

70 được xác định,

sh

tsc --explainFiles

01 sẽ được tính toán; Nhưng khi

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

70 là

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57, hãy dừng những gì chúng tôi làm và chỉ trả lại

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57.

Nói chung hơn, đoạn mã đó giống như viết như sau.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

25

Lưu ý rằng nếu

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

55 là

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57, mã của chúng tôi vẫn sẽ gặp lỗi khi truy cập

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

51. Tương tự như vậy, nếu

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

51 là

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57, chúng tôi sẽ gặp lỗi tại trang web cuộc gọi.

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

47 Chỉ kiểm tra xem giá trị ở bên trái của nó là

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hay

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 - không phải bất kỳ thuộc tính nào tiếp theo.

Bạn có thể thấy mình đang sử dụng

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

47 để thay thế nhiều mã thực hiện kiểm tra vô hiệu hóa lặp đi lặp lại bằng toán tử

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

49.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

26

Hãy nhớ rằng

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

47 hoạt động khác với các hoạt động

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

49 vì

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

49 sẽ hoạt động đặc biệt đối với các giá trị giả tưởng của Hồi giáo (ví dụ: chuỗi trống,

sh

tsc --explainFiles

21,

sh

tsc --explainFiles

22, và,

sh

tsc --explainFiles

23), nhưng đây là một tính năng có chủ ý của cấu trúc. Nó không phải là ngắn mạch trên dữ liệu hợp lệ như

sh

tsc --explainFiles

21 hoặc chuỗi trống.

Chuỗi tùy chọn cũng bao gồm hai hoạt động khác. Trước tiên, có quyền truy cập phần tử tùy chọn hoạt động tương tự như quyền truy cập thuộc tính tùy chọn, nhưng cho phép chúng tôi truy cập các thuộc tính không nhận dạng (ví dụ: chuỗi, số và ký hiệu tùy ý):

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

27

Ngoài ra còn có cuộc gọi tùy chọn, cho phép chúng tôi gọi các biểu thức có điều kiện nếu chúng không phải là

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

28

Hành vi ngắn mạch của người Viking mà các chuỗi tùy chọn có quyền truy cập tài sản, cuộc gọi, truy cập phần tử hạn chế - nó không mở rộng ra xa hơn các biểu thức này. Nói cách khác,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

29

Không ngăn chặn cuộc gọi phân chia hoặc

sh

tsc --explainFiles

27 xảy ra. Nó tương đương với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

30

Điều đó có thể dẫn đến việc chia

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57, đó là lý do tại sao trong

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

51, sau đây là một lỗi.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

31

Thêm chi tiết, bạn có thể đọc về đề xuất và xem yêu cầu kéo ban đầu.

Nullish hợp tác

Sân chơi

Chuỗi tùy chọn là vấn đề #16 trên Trình theo dõi vấn đề của chúng tôi. Đối với bối cảnh, đã có hơn 23.000 vấn đề về trình theo dõi vấn đề TypeScript kể từ đó.

Ở cốt lõi của nó, chuỗi tùy chọn cho phép chúng tôi viết mã trong đó TypeScript có thể ngừng chạy một số biểu thức nếu chúng tôi chạy vào

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57. Ngôi sao của chương trình trong chuỗi tùy chọn là toán tử

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

47 mới để truy cập thuộc tính tùy chọn. Khi chúng tôi viết mã như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

32

Đây là một cách để nói rằng khi

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

70 được xác định,

sh

tsc --explainFiles

01 sẽ được tính toán; Nhưng khi

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

70 là

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57, hãy dừng những gì chúng tôi làm và chỉ trả lại

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57.

Nói chung hơn, đoạn mã đó giống như viết như sau.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

33

Lưu ý rằng nếu

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

55 là

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57, mã của chúng tôi vẫn sẽ gặp lỗi khi truy cập

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

51. Tương tự như vậy, nếu

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

51 là

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57, chúng tôi sẽ gặp lỗi tại trang web cuộc gọi.

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

47 Chỉ kiểm tra xem giá trị ở bên trái của nó là

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hay

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 - không phải bất kỳ thuộc tính nào tiếp theo.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

34

Khi

sh

tsc --explainFiles

41 được đặt thành

sh

tsc --explainFiles

21, trang sẽ đặt âm lượng thành

sh

tsc --explainFiles

43 ngoài ý muốn.

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

78 tránh một số hành vi ngoài ý muốn từ

sh

tsc --explainFiles

21,

sh

tsc --explainFiles

22 và

sh

tsc --explainFiles

47 bị coi là giá trị giả.

Chúng tôi nợ rất nhiều cảm ơn các thành viên cộng đồng Wenlu Wang và Titian Cernicova Dragomir vì đã thực hiện tính năng này! Để biết thêm chi tiết, hãy kiểm tra yêu cầu kéo của họ và kho lưu trữ đề xuất kết hợp nullish.

Chức năng khẳng định

Sân chơi

Có một bộ chức năng cụ thể mà

sh

tsc --explainFiles

48 là một lỗi nếu có điều gì đó bất ngờ xảy ra. Họ gọi là các chức năng khẳng định của người khác. Ví dụ, Node.js có chức năng chuyên dụng cho điều này được gọi là

sh

tsc --explainFiles

49.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

35

Trong ví dụ này nếu

sh

tsc --explainFiles

50 không bằng với

sh

tsc --explainFiles

51, thì

sh

tsc --explainFiles

49 sẽ ném

sh

tsc --explainFiles

53.

Các xác nhận trong JavaScript thường được sử dụng để bảo vệ chống lại các loại không đúng được truyền vào. Ví dụ,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

36

Thật không may trong bản thảo, các kiểm tra này không bao giờ có thể được mã hóa đúng. Đối với mã được gõ lỏng lẻo, điều này có nghĩa là TypeScript đã kiểm tra ít hơn và đối với mã hơi bảo thủ, nó thường buộc người dùng phải sử dụng các xác nhận loại.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

37

Thay thế là thay vào đó hãy viết lại mã để ngôn ngữ có thể phân tích nó, nhưng điều này không thuận tiện.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

38

Cuối cùng, mục tiêu của TypeScript là nhập các cấu trúc JavaScript hiện có theo cách ít gây rối nhất. Vì lý do đó, TypeScript 3.7 giới thiệu một khái niệm mới có tên là Chữ ký xác nhận, mô hình hóa các chức năng khẳng định này.

Loại chữ ký khẳng định đầu tiên mô hình theo cách mà chức năng Node ____ ____849 hoạt động. Nó đảm bảo rằng bất kỳ điều kiện nào đang được kiểm tra phải đúng với phần còn lại của phạm vi chứa.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

39

sh

tsc --explainFiles

55 nói rằng bất cứ điều gì được chuyển vào tham số

sh

tsc --explainFiles

56 phải đúng nếu

sh

tsc --explainFiles

49 trả về (vì nếu không thì nó sẽ gây ra lỗi). Điều đó có nghĩa là đối với phần còn lại của phạm vi, điều kiện đó phải là sự thật. Ví dụ, sử dụng chức năng khẳng định này có nghĩa là chúng tôi bắt được ví dụ

sh

tsc --explainFiles

58 ban đầu của chúng tôi.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

40

Loại chữ ký khẳng định khác không kiểm tra một điều kiện, nhưng thay vào đó hãy nói với TypeScript rằng một biến hoặc thuộc tính cụ thể có một loại khác.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

41

Tại đây

sh

tsc --explainFiles

59 đảm bảo rằng sau bất kỳ cuộc gọi nào đến

sh

tsc --explainFiles

60, bất kỳ biến nào được truyền vào sẽ được biết là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

42

Những chữ ký khẳng định này rất giống với chữ ký vị ngữ loại viết:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

43

Và giống như chữ ký vị ngữ loại, những chữ ký khẳng định này là vô cùng biểu cảm. Chúng ta có thể thể hiện một số ý tưởng khá tinh vi với những điều này.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

44

Để đọc thêm về chữ ký khẳng định, hãy xem yêu cầu kéo ban đầu.

Hỗ trợ tốt hơn cho các chức năng trả lại ____ 513

Là một phần của công việc cho chữ ký khẳng định, TypeScript cần phải mã hóa thêm về nơi và các chức năng nào được gọi. Điều này đã cho chúng tôi cơ hội mở rộng hỗ trợ cho một lớp chức năng khác: các chức năng trả về

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13.

Mục đích của bất kỳ chức năng nào trả về

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13 là nó không bao giờ trả lại. Nó chỉ ra rằng một ngoại lệ đã bị ném, xảy ra tình trạng lỗi tạm dừng hoặc chương trình đã thoát ra. Ví dụ:

sh

tsc --explainFiles

65 trong

sh

tsc --explainFiles

66 được chỉ định để trả về

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13.

Để đảm bảo rằng một hàm không bao giờ có khả năng trả về

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 hoặc được trả lại hiệu quả từ tất cả các đường dẫn mã, TypeScript cần một số tín hiệu cú pháp - hoặc là

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

39 hoặc

sh

tsc --explainFiles

48 ở cuối hàm. Vì vậy, người dùng thấy mình ________ 639-ing chức năng thất bại của họ.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

45

Bây giờ khi các chức năng trả lại ____ 513 này được gọi, TypeScript nhận ra rằng chúng ảnh hưởng đến biểu đồ luồng điều khiển và tài khoản cho chúng.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

46

Như với các chức năng khẳng định, bạn có thể đọc thêm ở cùng một yêu cầu kéo.

(Thêm) Bí danh loại đệ quy

Sân chơi

Có một bộ chức năng cụ thể mà

sh

tsc --explainFiles

48 là một lỗi nếu có điều gì đó bất ngờ xảy ra. Họ gọi là các chức năng khẳng định của người khác. Ví dụ, Node.js có chức năng chuyên dụng cho điều này được gọi là

sh

tsc --explainFiles

49.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

47

Trong ví dụ này nếu

sh

tsc --explainFiles

50 không bằng với

sh

tsc --explainFiles

51, thì

sh

tsc --explainFiles

49 sẽ ném

sh

tsc --explainFiles

53.

Các xác nhận trong JavaScript thường được sử dụng để bảo vệ chống lại các loại không đúng được truyền vào. Ví dụ,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

48

Điều này là lạ bởi vì về mặt kỹ thuật không có gì sai với bất kỳ người dùng sử dụng nào luôn có thể viết những gì hiệu quả là cùng một mã bằng cách giới thiệu một giao diện.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

49

Bởi vì các giao diện (và các loại đối tượng khác) giới thiệu một mức độ gián tiếp và cấu trúc đầy đủ của chúng không cần phải được xây dựng một cách háo hức, TypeScript không có vấn đề gì khi làm việc với cấu trúc này.

Nhưng cách giải quyết giới thiệu giao diện không trực quan cho người dùng. Và về nguyên tắc, thực sự không có gì sai với phiên bản gốc của

sh

tsc --explainFiles

78 đã sử dụng trực tiếp

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

19. Nếu trình biên dịch là một chút của Lazier, và chỉ tính toán các đối số loại thành

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

19 khi cần thiết, thì TypeScript có thể thể hiện chính xác các đối số này.

Đó chính xác là những gì TypeScript 3.7 giới thiệu. Ở cấp độ cao nhất của một bí danh, TypeScript sẽ trì hoãn việc giải quyết các đối số loại để cho phép các mẫu này.

Điều này có nghĩa là mã giống như sau đây đang cố gắng đại diện cho JSON

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

50

Cuối cùng có thể được viết lại mà không có giao diện trợ giúp.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

51

Thư giãn mới này cũng cho phép chúng ta tham khảo các bí danh loại tham chiếu đệ quy trong các bộ dữ liệu. Mã sau đây được sử dụng cho lỗi hiện là mã TypeScript hợp lệ.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

52

Để biết thêm thông tin, bạn có thể đọc theo yêu cầu kéo ban đầu.

shtsc --explainFiles81 và shtsc --explainFiles82

Cờ

sh

tsc --explainFiles

83 trong TypeScript cho phép chúng tôi tạo các tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 (tệp khai báo) từ các tệp nguồn TypeScript (tức là các tệp

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

45 và

sh

tsc --explainFiles

86). Các tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 này rất quan trọng vì một vài lý do.

Trước hết, họ rất quan trọng vì chúng cho phép TypeScript kiểm tra loại chống lại các dự án khác mà không kiểm tra lại mã nguồn gốc. Họ cũng rất quan trọng vì chúng cho phép TypeScript tương tác với các thư viện JavaScript hiện có mà weren được xây dựng với TypeScript trong tâm trí. Cuối cùng, một lợi ích thường được đánh giá thấp: cả người dùng TypeScript và JavaScript có thể được hưởng lợi từ các tệp này khi sử dụng các trình chỉnh sửa được cung cấp bởi TypeScript để có những thứ như tự động hoàn thành tốt hơn.

Thật không may,

sh

tsc --explainFiles

83 đã làm việc với cờ

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

45 cho phép trộn các tệp đầu vào TypeScript và JavaScript. Đây là một giới hạn bực bội vì điều đó có nghĩa là người dùng không thể sử dụng cờ

sh

tsc --explainFiles

83 khi di chuyển các cơ sở mã, ngay cả khi chúng được phát hiện bằng JSDOC. TypeScript 3.7 Thay đổi và cho phép hai tùy chọn được sử dụng cùng nhau!

Kết quả có ảnh hưởng nhất của tính năng này có thể hơi tinh tế: với TypeScript 3.7, người dùng có thể viết thư viện trong JSDOC chú thích JavaScript và hỗ trợ người dùng TypeScript.

Cách mà điều này hoạt động là khi sử dụng

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

45, TypeScript có một số phân tích hiệu ứng tốt nhất để hiểu các mẫu JavaScript thông thường; Tuy nhiên, cách mà một số mẫu được thể hiện trong JavaScript don don nhất thiết trông giống như tương đương của chúng trong TypeScript. Khi

sh

tsc --explainFiles

83 phát ra được bật, các bản thảo đã tìm ra cách tốt nhất để chuyển đổi các bình luận của JSDOC và xuất khẩu phổ biến thành khai báo loại hợp lệ và tương tự trong các tệp đầu ra

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19.

Ví dụ, đoạn mã sau đây

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

53

Sẽ tạo một tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

54

Điều này có thể vượt xa các chức năng cơ bản với các thẻ

sh

tsc --explainFiles

95, trong đó ví dụ sau:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

55

sẽ được chuyển thành tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 sau:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

56

Lưu ý rằng khi sử dụng các cờ này với nhau, TypeScript không nhất thiết phải làm giảm các tệp

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

20. Nếu bạn chỉ đơn giản muốn TypeScript để tạo các tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19, bạn có thể sử dụng tùy chọn trình biên dịch

sh

tsc --explainFiles

99.

Để biết thêm chi tiết, bạn có thể kiểm tra yêu cầu kéo ban đầu.

Cờ sh# Forward output to a text filetsc --explainFiles > expanation.txt# Pipe output to a utility program like `less`, or an editor like VS Codetsc --explainFiles | lesstsc --explainFiles | code -00 và công cụ sửa đổi thuộc tính sh# Forward output to a text filetsc --explainFiles > expanation.txt# Pipe output to a utility program like `less`, or an editor like VS Codetsc --explainFiles | lesstsc --explainFiles | code -01

Quay lại khi TypeScript triển khai các trường lớp công khai, chúng tôi giả định với khả năng tốt nhất của chúng tôi rằng mã sau

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

57

sẽ tương đương với một nhiệm vụ tương tự trong một cơ quan xây dựng.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

58

Thật không may, trong khi đây dường như là hướng mà đề xuất chuyển sang trong những ngày đầu, có một cơ hội cực kỳ mạnh mẽ rằng các lĩnh vực lớp công cộng sẽ được tiêu chuẩn hóa khác nhau. Thay vào đó, mẫu mã ban đầu có thể cần phải khử cách một cái gì đó gần hơn với các mục sau hơn:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

59

Mặc dù TypeScript 3.7 không phải là thay đổi bất kỳ phát ra hiện tại nào theo mặc định, chúng tôi đã tung ra các thay đổi tăng dần để giúp người dùng giảm thiểu khả năng phá vỡ tiềm năng trong tương lai. Chúng tôi đã cung cấp một lá cờ mới có tên

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

00 để kích hoạt chế độ phát ra này với một số logic kiểm tra mới.

Hai thay đổi lớn nhất là như sau:

  • Khai báo được khởi tạo với

    sh

    # Forward output to a text file

    tsc --explainFiles > expanation.txt

    # Pipe output to a utility program like `less`, or an editor like VS Code

    tsc --explainFiles | less

    tsc --explainFiles | code -

    03.
  • Khai báo luôn được khởi tạo thành

    ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    57, ngay cả khi chúng không có bộ khởi tạo.

Điều này có thể gây ra khá nhiều Fallout cho mã hiện có sử dụng kế thừa. Trước hết,

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

94 người truy cập từ các lớp cơ sở đã giành được kích hoạt - họ sẽ hoàn toàn bị ghi đè.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

60

Thứ hai, sử dụng các trường lớp để chuyên về các thuộc tính từ các lớp cơ sở cũng đã giành được công việc.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

61

Điều mà hai người này sôi sục là việc trộn các thuộc tính với người truy cập sẽ gây ra các vấn đề, và do đó, các thuộc tính sẽ không ghi lại mà không có bộ khởi tạo.

Để phát hiện sự cố xung quanh Trình truy cập, TypeScript 3.7 hiện sẽ phát ra ________ 693/________ 694 Trình truy cập trong các tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 để trong TypeScript có thể kiểm tra trình truy cập ghi đè.

Mã mà LỚN bị ảnh hưởng bởi sự thay đổi của các trường lớp có thể giải quyết vấn đề bằng cách chuyển đổi các bộ khởi tạo trường sang các bài tập trong các cơ quan cấu trúc.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

62

Để giúp giảm thiểu vấn đề thứ hai, bạn có thể thêm trình khởi tạo rõ ràng hoặc thêm công cụ sửa đổi

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

01 để chỉ ra rằng một thuộc tính không nên phát ra.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

63

Hiện tại

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

00 chỉ khả dụng khi nhắm mục tiêu ES5 trở lên, vì

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

03 không tồn tại trong ES3. Để đạt được kiểm tra tương tự các vấn đề, bạn có thể tạo một dự án riêng biệt nhắm mục tiêu ES5 và sử dụng

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

12 để tránh xây dựng đầy đủ.

Để biết thêm thông tin, bạn có thể xem yêu cầu kéo ban đầu cho những thay đổi này.

Chúng tôi khuyến khích người dùng thử cờ

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

00 và báo cáo lại về trình theo dõi vấn đề của chúng tôi hoặc trong các ý kiến ​​dưới đây. Điều này bao gồm phản hồi về khó khăn trong việc áp dụng cờ để chúng ta có thể hiểu làm thế nào chúng ta có thể làm cho việc di chuyển dễ dàng hơn.

Chỉnh sửa không xây dựng với các tài liệu tham khảo dự án

Tài liệu tham khảo dự án TypeScript cung cấp cho chúng tôi một cách dễ dàng để phá vỡ các mã hóa để cung cấp cho chúng tôi các biên dịch nhanh hơn. Thật không may, chỉnh sửa một dự án có sự phụ thuộc đã được xây dựng (hoặc đầu ra đã hết hạn) có nghĩa là trải nghiệm chỉnh sửa sẽ hoạt động tốt.

Trong TypeScript 3.7, khi mở một dự án với các phụ thuộc, TypeScript sẽ tự động sử dụng các tệp ________ 645/________ 886 thay thế. Điều này có nghĩa là các dự án sử dụng tài liệu tham khảo dự án giờ đây sẽ thấy trải nghiệm chỉnh sửa được cải thiện trong đó các hoạt động ngữ nghĩa được cập nhật và chỉ cần làm việc. Bạn có thể vô hiệu hóa hành vi này với tùy chọn trình biên dịch

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

16 có thể phù hợp khi làm việc trong các dự án rất lớn, nơi thay đổi này có thể ảnh hưởng đến hiệu suất chỉnh sửa.

Bạn có thể đọc thêm về thay đổi này bằng cách đọc theo yêu cầu kéo của nó.

Kiểm tra chức năng chưa được đặt

Một lỗi phổ biến và nguy hiểm là quên gọi một hàm, đặc biệt nếu hàm không có đối số hoặc được đặt tên theo cách ngụ ý nó có thể là một thuộc tính chứ không phải là một hàm.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

64

Ở đây, chúng tôi quên gọi

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

17 và mã không chính xác cho phép người dùng không phải là người điều hành chỉnh sửa cấu hình!

Trong TypeScript 3.7, điều này được xác định là lỗi có thể xảy ra:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

65

Kiểm tra này là một sự thay đổi phá vỡ, nhưng vì lý do đó, việc kiểm tra rất bảo thủ. Lỗi này chỉ được phát hành trong các điều kiện

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

82 và nó không được phát hành trên các thuộc tính tùy chọn, nếu

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

51 bị tắt hoặc nếu chức năng được gọi sau đó trong phần thân của

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

82:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

66

Nếu bạn có ý định kiểm tra chức năng mà không gọi nó, bạn có thể sửa định nghĩa của nó để bao gồm ________ 457/________ 607 hoặc sử dụng

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

23 để viết một cái gì đó như

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

24 để chỉ ra rằng sự ép buộc là có chủ ý.

Chúng tôi nợ rất nhiều lời cảm ơn đến người dùng GitHub @jwbay, người đã chủ động tạo ra một bằng chứng về khái niệm và lặp đi lặp lại để cung cấp cho chúng tôi phiên bản hiện tại.

sh# Forward output to a text filetsc --explainFiles > expanation.txt# Pipe output to a utility program like `less`, or an editor like VS Codetsc --explainFiles | lesstsc --explainFiles | code -25 trong các tệp TypeScript

TypeScript 3.7 cho phép chúng tôi thêm các nhận xét

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

25 vào đầu các tệp TypeScript để vô hiệu hóa kiểm tra ngữ nghĩa. Trong lịch sử, nhận xét này chỉ được tôn trọng trong các tệp nguồn JavaScript với sự hiện diện của

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

44, nhưng chúng tôi đã mở rộng hỗ trợ cho các tệp TypeScript để giúp di chuyển dễ dàng hơn cho tất cả người dùng.

Tùy chọn định dạng bán kết

Kiểu định dạng tích hợp TypeScript hiện hỗ trợ chèn và loại bỏ dấu chấm phẩy tại các vị trí mà dấu chấm phẩy kéo dài là tùy chọn do các quy tắc chèn bán tự động (ASI) tự động của JavaScript. Cài đặt hiện có sẵn trong Visual Studio Code Insiders và sẽ có sẵn trong Visual Studio 16.4 Xem trước 2 trong menu Tùy chọn Công cụ.

Hướng dẫn how typescript disinfect html? - cách loại bỏ tập lệnh html?

Việc chọn một giá trị của chèn chèn vào hoặc loại bỏ, cũng ảnh hưởng đến định dạng của các phiên bản tự động, các loại được trích xuất và mã được tạo khác được cung cấp bởi các dịch vụ TypeScript. Để lại cài đặt trên giá trị mặc định của nó là bỏ qua, làm cho mã được tạo ra khớp với ưu tiên dấu chấm phẩy được phát hiện trong tệp hiện tại.

3.7 Phá vỡ thay đổi

DOM thay đổi

Các loại trong

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

28 đã được cập nhật. Những thay đổi này phần lớn là những thay đổi chính xác liên quan đến khả năng vô hiệu hóa, nhưng cuối cùng tác động sẽ phụ thuộc vào cơ sở mã của bạn.

Giảm thiểu trường lớp

Như đã đề cập ở trên, TypeScript 3.7 phát ra ________ 693/________ 694 Trình truy cập trong các tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 có thể gây ra sự phá vỡ thay đổi cho người tiêu dùng trên các phiên bản cũ hơn của TypeScript như 3.5 trở lên. Người dùng TypeScript 3.6 sẽ không bị ảnh hưởng, vì phiên bản đó được chứng minh trong tương lai cho tính năng này.

Mặc dù không bị vỡ mỗi se, nhưng việc chọn cờ

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

00 có thể gây ra vỡ khi:

  • Ghi đè người truy cập trong một lớp có nguồn gốc với khai báo thuộc tính
  • khai báo lại một tuyên bố tài sản mà không có trình khởi tạo

Để hiểu toàn bộ tác động, hãy đọc phần trên trên cờ

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

00.

Chức năng kiểm tra sự thật

Như đã đề cập ở trên, các bản thảo bây giờ lỗi khi các chức năng dường như chưa được xác định trong các điều kiện câu lệnh

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

82. Một lỗi được phát hành khi một loại chức năng được kiểm tra trong các điều kiện

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

82 trừ khi có bất kỳ điều gì sau đây áp dụng:

  • Giá trị đã kiểm tra đến từ một thuộc tính tùy chọn
  • ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    51 bị vô hiệu hóa
  • Chức năng này sau đó được gọi trong phần thân của

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    82

Khai báo loại địa phương và nhập khẩu hiện đang xung đột

Do lỗi, cấu trúc sau đây được cho phép trước đây trong TypeScript:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

67

Ở đây,

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

38 dường như bắt nguồn từ cả tuyên bố

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

51 và tuyên bố

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

40 địa phương. Có lẽ đáng ngạc nhiên, bên trong mô -đun,

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

38 chỉ đề cập đến định nghĩa

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

51ed và khai báo cục bộ

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

38 chỉ có thể sử dụng khi được nhập từ một tệp khác. Điều này rất khó hiểu và đánh giá của chúng tôi về số lượng rất nhỏ các trường hợp mã như thế này trong tự nhiên cho thấy các nhà phát triển thường nghĩ rằng có điều gì đó khác biệt đang xảy ra.

Trong TypeScript 3.7, điều này hiện được xác định chính xác là lỗi định danh trùng lặp. Bản sửa lỗi chính xác phụ thuộc vào mục đích ban đầu của tác giả và nên được giải quyết trên cơ sở từng trường hợp cụ thể. Thông thường, xung đột đặt tên là không chủ ý và cách khắc phục tốt nhất là đổi tên loại nhập khẩu. Nếu ý định là tăng cường loại nhập khẩu, một mô -đun thích hợp nên được viết thay thế.

3.7 Thay đổi API

Để cho phép các mẫu bí danh loại đệ quy được mô tả ở trên, thuộc tính

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

44 đã bị xóa khỏi giao diện

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

45. Thay vào đó, người dùng nên sử dụng chức năng

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

46 trên các trường hợp

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

47.

TypeScript 3.6

Máy phát điện chặt chẽ hơn

TypeScript 3.6 Giới thiệu kiểm tra chặt chẽ hơn cho các trình lặp và chức năng máy phát. Trong các phiên bản trước, người dùng của máy phát điện không có cách nào để phân biệt xem một giá trị được mang lại hay trả về từ một trình tạo.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

68

Ngoài ra, các máy phát điện chỉ giả định loại

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

69 luôn là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

72.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

69

Trong TypeScript 3.6, trình kiểm tra bây giờ biết rằng loại chính xác cho

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

50 phải là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79 trong ví dụ đầu tiên của chúng tôi và sẽ lỗi chính xác trong cuộc gọi của chúng tôi đến

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

52 trong ví dụ cuối cùng của chúng tôi. Điều này là nhờ một số thay đổi trong khai báo loại

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

53 và

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

54 để bao gồm một vài tham số loại mới và loại mới mà TypeScript sử dụng để đại diện cho các trình tạo được gọi là loại

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

55.

Loại

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

53 hiện cho phép người dùng chỉ định loại mang lại, loại được trả về và loại mà

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

57 có thể chấp nhận.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

70

Dựa trên công việc đó, loại

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

55 mới là một

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

53 luôn có cả các phương pháp

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

39 và

sh

tsc --explainFiles

48 hiện tại, và cũng có thể hiểu được.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

71

Để cho phép sự khác biệt giữa các giá trị được trả về và các giá trị mang lại, TypeScript 3.6 đã chuyển đổi loại

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

54 thành loại liên minh phân biệt đối xử:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

72

Nói tóm lại, điều này có nghĩa là bạn sẽ có thể thu hẹp các giá trị xuống một cách thích hợp từ các trình lặp khi giao dịch trực tiếp với chúng.

Để biểu diễn chính xác các loại có thể được truyền vào một trình tạo từ các cuộc gọi đến

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

52, TypeScript 3.6 cũng đưa ra một số cách sử dụng

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

69 trong phần thân của hàm tạo.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

73

Nếu bạn thích rõ ràng, bạn cũng có thể thực thi các loại giá trị có thể được trả về, mang lại và đánh giá từ các biểu thức

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

69 bằng cách sử dụng loại trả về rõ ràng. Dưới đây,

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

52 chỉ có thể được gọi bằng

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

67 và tùy thuộc vào giá trị của

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

68,

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

69 là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79 hoặc

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

06.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

74

Để biết thêm chi tiết về thay đổi, hãy xem yêu cầu kéo ở đây.

Sự lây lan mảng chính xác hơn

Trong các mục tiêu trước ES2015, giá trị trung thực nhất cho các cấu trúc như ________ 638/________ 973 vòng lặp và chênh lệch mảng có thể hơi nặng. Vì lý do này, TypeScript sử dụng một sự phát ra đơn giản hơn theo mặc định chỉ hỗ trợ các loại mảng và hỗ trợ lặp lại trên các loại khác bằng cờ

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

74. Mặc định lỏng hơn mà không có

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

74 hoạt động khá tốt; Tuy nhiên, có một số trường hợp phổ biến trong đó sự biến đổi của các chênh lệch mảng có sự khác biệt có thể quan sát được. Ví dụ: mảng sau đây chứa một sự lây lan

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

75

có thể được viết lại dưới dạng mảng sau đây

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

76

Tuy nhiên, thay vào đó, TypeScript sẽ chuyển đổi mã gốc thành mã này:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

77

mà hơi khác nhau.

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

76 tạo ra một mảng có chiều dài 5, nhưng không có khe cắm thuộc tính xác định.

TypeScript 3.6 giới thiệu một người trợ giúp

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

77 mới để mô hình chính xác những gì xảy ra trong ECMAScript 2015 trong các mục tiêu cũ hơn

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

74.

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

77 cũng có sẵn trong TSLIB.

Để biết thêm thông tin, hãy xem yêu cầu kéo có liên quan.

Cải thiện UX xung quanh những lời hứa

TypeScript 3.6 giới thiệu một số cải tiến khi

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

16 bị xử lý sai.

Ví dụ, nó thường rất phổ biến để quên

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

15 hoặc

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

42 Nội dung của

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

16 trước khi chuyển nó sang chức năng khác. Thông báo lỗi của TypeScript hiện đang được chuyên dụng và thông báo cho người dùng rằng có lẽ họ nên xem xét sử dụng từ khóa

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

42.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

78

Nó cũng phổ biến để cố gắng truy cập một phương thức trước ________ 742-ing hoặc ________ 515-ing A

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

16. Đây là một ví dụ khác, trong số nhiều người khác, nơi chúng tôi có thể làm tốt hơn.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

79

Để biết thêm chi tiết, hãy xem vấn đề khởi tạo, cũng như các yêu cầu kéo liên kết lại với nó.

Hỗ trợ unicode tốt hơn cho số nhận dạng

TypeScript 3.6 chứa hỗ trợ tốt hơn cho các ký tự Unicode trong các định danh khi phát ra các mục tiêu ES2015 và sau đó.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

80

TypeScript 3.6 hỗ trợ chuyển đổi

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

88 thành

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

89 khi mục tiêu

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

58 của bạn được đặt thành

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

60.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

81

tsabstract class Shape { abstract getArea(): number;}// ---cut---interface HasArea { getArea(): number;}// Works!let Ctor: abstract new () => HasArea = Shape;93 và tsabstract class Shape { abstract getArea(): number;}// ---cut---interface HasArea { getArea(): number;}// Works!let Ctor: abstract new () => HasArea = Shape;94 Người truy cập được phép trong bối cảnh xung quanh

Trong các phiên bản trước của TypeScript, ngôn ngữ didn cho phép

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

93 và

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

94 Trình truy cập trong bối cảnh xung quanh (như trong các lớp ________ 901-D hoặc trong các tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 nói chung). Lý do là người truy cập khác biệt với các thuộc tính cho đến khi viết và đọc cho các thuộc tính này; Tuy nhiên, vì đề xuất trường lớp Ecmascript có thể có hành vi khác nhau so với các phiên bản hiện có của TypeScript, chúng tôi nhận ra rằng chúng tôi cần một cách để truyền đạt hành vi khác nhau này để cung cấp các lỗi phù hợp trong các lớp con.

Do đó, người dùng có thể viết getters và setters trong bối cảnh xung quanh trong TypeScript 3.6.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

82

Trong TypeScript 3.7, bản thân trình biên dịch sẽ tận dụng tính năng này để các tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 được tạo cũng sẽ phát ra ________ 693/________ 694.

Các lớp và chức năng xung quanh có thể hợp nhất

Trong các phiên bản trước của TypeScript, đó là một lỗi để hợp nhất các lớp và chức năng trong mọi trường hợp. Bây giờ, các lớp và chức năng xung quanh (các lớp/chức năng với công cụ sửa đổi

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

01 hoặc trong các tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19) có thể hợp nhất. Điều này có nghĩa là bây giờ bạn có thể viết như sau:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

83

thay vì cần sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

84

Một lợi thế của điều này là mẫu hàm tạo có thể gọi có thể được thể hiện dễ dàng trong khi cũng cho phép các không gian tên hợp nhất với các khai báo này (vì các khai báo

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

003 có thể hợp nhất với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

004s).

Trong TypeScript 3.7, trình biên dịch sẽ tận dụng tính năng này để các tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 được tạo từ các tệp

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

20 có thể chụp một cách thích hợp cả khả năng gọi và khả năng xây dựng của hàm giống như lớp.

Để biết thêm chi tiết, hãy xem PR ban đầu trên GitHub.

API để hỗ trợ ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];007 và ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];008

TypeScript 3.0 đã giới thiệu hỗ trợ để tham khảo khác và xây dựng chúng dần dần bằng cách sử dụng cờ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

007. Ngoài ra, TypeScript 3.4 đã giới thiệu cờ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

010 để lưu thông tin về các biên dịch trước đó chỉ xây dựng lại một số tệp nhất định. Những lá cờ này cực kỳ hữu ích để cấu trúc các dự án linh hoạt hơn và tăng tốc tích tụ. Thật không may, việc sử dụng các cờ này đã không hoạt động với các công cụ xây dựng bên thứ 3 như Gulp và Webpack. TypeScript 3.6 hiện phơi bày hai bộ API để vận hành trên các tài liệu tham khảo dự án và xây dựng chương trình gia tăng.

Để tạo các bản dựng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

010, người dùng có thể tận dụng API

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

012 và ____1013. Người dùng cũng có thể ngắt lại các phiên bản chương trình cũ từ các tệp

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

014 được tạo bởi API này bằng cách sử dụng hàm

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

015 mới được hiển thị, điều này chỉ được sử dụng để tạo các chương trình mới (tức là bạn không thể sửa đổi phiên bản đã trả lại - nó chỉ có nghĩa là chỉ có nghĩa là được sử dụng cho tham số

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

016 trong các chức năng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

017 khác).

Để tận dụng các tài liệu tham khảo dự án, hàm

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

018 mới đã được phơi bày, trả về một ví dụ của loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

019 mới.

Để biết thêm chi tiết về các API này, bạn có thể thấy yêu cầu kéo ban đầu.

Biểu phần nhận biết mã chỉnh sửa

Các biên tập viên như Visual Studio và Visual Studio Code có thể tự động áp dụng các bản sửa lỗi nhanh, tái cấu trúc và các biến đổi khác như tự động nhập các giá trị từ các mô -đun khác. Các phép biến đổi này được cung cấp bởi TypeScript và các phiên bản cũ hơn của các dấu chấm phẩy được thêm vào một cách vô điều kiện vào cuối mỗi câu lệnh; Thật không may, điều này không đồng ý với nhiều hướng dẫn kiểu người dùng và nhiều người dùng đã không hài lòng với trình soạn thảo chèn dấu chấm phẩy.

TypeScript hiện đủ thông minh để phát hiện liệu tệp của bạn có sử dụng dấu chấm phẩy hay không khi áp dụng các loại chỉnh sửa này. Nếu tập tin của bạn thường thiếu dấu chấm phẩy, TypeScript won sẽ thêm một.

Để biết thêm chi tiết, xem yêu cầu kéo tương ứng.

Cú pháp tự động thông minh hơn

JavaScript có rất nhiều cú pháp hoặc quy ước mô -đun khác nhau: cái trong tiêu chuẩn ECMAScript, một nút đã hỗ trợ (CommonJS), AMD, System.js, v.v. Đối với hầu hết các phần, TypeScript sẽ mặc định để tự động nhập tự động bằng cách sử dụng cú pháp mô-đun Ecmascript, thường không phù hợp trong một số dự án TypeScript với các cài đặt trình biên dịch khác nhau hoặc trong các dự án nút có các cuộc gọi JavaScript và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

020 đơn giản.

TypeScript 3.6 hiện đang thông minh hơn một chút về việc xem xét nhập khẩu hiện tại của bạn trước khi quyết định cách tự động thực hiện các mô-đun khác. Bạn có thể xem thêm chi tiết trong yêu cầu kéo ban đầu ở đây.

tsabstract class SuperClass { abstract someMethod(): void; badda() {}}type AbstractConstructor = abstract new (...args: any[]) => Tfunction withStyles>(Ctor: T) { abstract class StyledClass extends Ctor { getStyles() { // ... } } return StyledClass;}class SubClass extends withStyles(SuperClass) { someMethod() { this.someMethod() }}42 Hoàn thành về lời hứa

Sân chơi TypeScript mới

Sân chơi TypeScript đã nhận được một sự làm mới rất cần thiết với chức năng mới tiện dụng! Sân chơi mới phần lớn là một ngã ba của sân chơi TypeScript Artem Tyurin, mà các thành viên cộng đồng đã sử dụng ngày càng nhiều. Chúng tôi nợ Artem một lời cảm ơn lớn vì đã giúp đỡ ở đây!

Sân chơi mới hiện hỗ trợ nhiều tùy chọn mới bao gồm:

  • Tùy chọn

    ts

    abstract class SuperClass {

    abstract someMethod(): void;

    badda() {}

    }

    type AbstractConstructor<T> = abstract new (...args: any[]) => T

    function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

    abstract class StyledClass extends Ctor {

    getStyles() {

    // ...

    }

    }

    return StyledClass;

    }

    class SubClass extends withStyles(SuperClass) {

    someMethod() {

    this.someMethod()

    }

    }

    56 (cho phép người dùng chuyển ra khỏi

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    023 sang

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    024,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    025,

    ts

    abstract class SuperClass {

    abstract someMethod(): void;

    badda() {}

    }

    type AbstractConstructor<T> = abstract new (...args: any[]) => T

    function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

    abstract class StyledClass extends Ctor {

    getStyles() {

    // ...

    }

    }

    return StyledClass;

    }

    class SubClass extends withStyles(SuperClass) {

    someMethod() {

    this.someMethod()

    }

    }

    59, v.v.)
  • Tất cả các cờ nghiêm ngặt (bao gồm cả

    ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    30)
  • Hỗ trợ cho các tệp JavaScript đơn giản (sử dụng

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    028 và tùy chọn

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    44)

Các tùy chọn này cũng tồn tại khi chia sẻ các liên kết đến các mẫu sân chơi, cho phép người dùng chia sẻ các ví dụ đáng tin cậy hơn mà không phải nói với người nhận, Oh, đừng quên bật tùy chọn

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

68!

Trong tương lai gần, chúng tôi sẽ làm mới các mẫu sân chơi, thêm hỗ trợ JSX và đánh bóng loại mua tự động, có nghĩa là bạn sẽ có thể thấy trải nghiệm tương tự trên sân chơi như bạn có trong trình soạn thảo cá nhân của mình .

Khi chúng tôi cải thiện sân chơi và trang web, chúng tôi hoan nghênh các yêu cầu phản hồi và kéo trên GitHub!

TypeScript 3.5

Cải tiến tốc độ

TypeScript 3.5 giới thiệu một số tối ưu hóa xung quanh việc kiểm tra loại và các bản dựng gia tăng.

Tăng tốc kiểm tra loại

TypeScript 3.5 chứa các tối ưu hóa nhất định qua TypeScript 3.4 để kiểm tra loại hiệu quả hơn. Những cải tiến này rõ rệt hơn đáng kể trong các kịch bản biên tập trong đó các hoạt động kiểm tra loại như danh sách hoàn thành mã.

ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];008 Cải tiến

TypeScript 3.5 cải thiện chế độ xây dựng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

010 của 3.4, bằng cách lưu thông tin về cách tính trạng thái của thế giới - cài đặt trình biên dịch, tại sao các tệp được tra cứu, nơi tìm thấy các tệp, v.v. Trong chế độ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

007, chúng tôi đã phát hiện ra rằng lượng thời gian xây dựng lại có thể giảm tới 68% so với TypeScript 3.4!

Để biết thêm chi tiết, bạn có thể thấy các yêu cầu kéo đến

  • Độ phân giải mô -đun bộ đệm
  • Cài đặt bộ nhớ cache được tính từ

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    98

Loại trợ giúp tsinterface Options { /** File patterns to be excluded. */ exclude?: string[]; /** * It handles any extra properties that we haven't declared as type 'any'. */ [x: string]: any;}function processOptions(opts: Options) { // Notice we're *intentionally* accessing `excludes`, not `exclude` if (opts.excludes) { console.error( "The option `excludes` is not valid. Did you mean `exclude`?" ); }}14

TypeScript 3.5 giới thiệu loại người trợ giúp

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

14 mới, tạo ra một loại mới với một số thuộc tính được bỏ từ bản gốc.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

85

Ở đây chúng tôi có thể sao chép tất cả các thuộc tính của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

037 ngoại trừ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

038 bằng cách sử dụng trình trợ giúp

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

14.

Để biết thêm chi tiết, hãy xem yêu cầu kéo trên GitHub để thêm

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

14, cũng như thay đổi để sử dụng

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

14 cho phần còn lại của đối tượng.

Cải thiện kiểm tra tài sản dư thừa trong các loại liên minh

Trong TypeScript 3.4 và sớm hơn, một số thuộc tính dư thừa được cho phép trong các tình huống mà chúng thực sự không nên có. Chẳng hạn, TypeScript 3.4 cho phép thuộc tính

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

042 không chính xác trong đối tượng theo nghĩa đen mặc dù các loại của nó không khớp giữa

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

043 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

044.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

86

Trước đây, một công đoàn không bị coi thường sẽ có bất kỳ kiểm tra tài sản vượt mức nào được thực hiện trên các thành viên của mình và kết quả là, thuộc tính

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

042 được gõ không chính xác bị trượt.

Trong TypeScript 3.5, trình kiểm tra loại ít nhất là xác minh rằng tất cả các thuộc tính được cung cấp thuộc về một số thành viên công đoàn và có loại thích hợp, có nghĩa là mẫu trên có lỗi chính xác.

Lưu ý rằng sự chồng chéo một phần vẫn được phép miễn là các loại tài sản là hợp lệ.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

87

Cờ ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];046

Trong TypeScript 3.5, bây giờ bạn có thể tham khảo các khai báo toàn cầu của UMD như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

88

Từ bất cứ đâu - thậm chí các mô -đun - sử dụng cờ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

047 mới.

Chế độ này bổ sung tính linh hoạt cho việc trộn và kết hợp theo cách các thư viện của bên thứ 3, nơi các thư viện mà các thư viện tuyên bố luôn có thể được tiêu thụ, ngay cả từ trong các mô -đun.

Để biết thêm chi tiết, hãy xem yêu cầu kéo trên GitHub.

Kiểm tra loại công đoàn thông minh hơn

Trong TypeScript 3.4 và trước, ví dụ sau sẽ thất bại:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

89

Điều đó vì

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

048 không thể gán cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

049 cũng như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

050. Tại sao? Bởi vì tài sản

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

68 trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

048 không đủ cụ thể - nên nó ____967 trong khi mỗi thành phần của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 có một thuộc tính

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

68 mà cụ thể là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

056 hoặc

sh

tsc --explainFiles

23. Đó là những gì chúng tôi muốn nói bởi mỗi loại cấu thành được kiểm tra trong sự cô lập: TypeScript không chỉ liên kết từng tài sản với nhau và xem liệu

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

048 có thể được gán cho điều đó không. Nếu có, một số mã xấu có thể vượt qua như sau:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

90

Tuy nhiên, điều này là một chút quá nghiêm ngặt đối với ví dụ ban đầu. Nếu bạn tìm ra loại chính xác của bất kỳ giá trị có thể có của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

048, bạn thực sự có thể thấy rằng nó phù hợp chính xác với các loại trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054.

Trong TypeScript 3.5, khi gán cho các loại có thuộc tính phân biệt đối xử như trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054, ngôn ngữ thực sự sẽ đi xa hơn và phân tách các loại như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

048 thành một liên minh của mọi loại cư dân có thể. Trong trường hợp này, vì

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

67 là một liên minh của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

056 và

sh

tsc --explainFiles

23,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

048 sẽ được xem là một liên minh của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

049 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

050.

Để biết thêm chi tiết, bạn có thể thấy yêu cầu kéo ban đầu trên GitHub.

Suy luận loại bậc cao hơn từ các hàm tạo chung

Trong TypeScript 3.4, chúng tôi đã cải thiện suy luận khi các hàm chung trả về các chức năng như vậy:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

91

lấy các chức năng chung khác làm đối số, như vậy:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

92

Thay vì một loại tương đối vô dụng như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

069, mà các phiên bản cũ hơn của ngôn ngữ sẽ suy ra, suy luận của TypeScript 3.4 cho phép

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

070 là chung chung. Loại mới của nó là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

071.

TypeScript 3.5 khái quát hóa hành vi này để làm việc trên các chức năng của hàm tạo.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

93

Ngoài các mẫu thành phần như trên, suy luận mới này về các hàm tạo chung có nghĩa là các chức năng hoạt động trên các thành phần lớp trong các thư viện UI nhất định như React có thể hoạt động chính xác hơn trên các thành phần lớp chung.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

94

Để tìm hiểu thêm, hãy xem yêu cầu kéo ban đầu trên GitHub.

TypeScript 3.4

Các bản dựng tiếp theo nhanh hơn với cờ ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];008

TypeScript 3.4 Giới thiệu một lá cờ mới có tên

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

010, nói với TypeScript để lưu thông tin về biểu đồ dự án từ phần tổng hợp cuối cùng. Lần tiếp theo TypeScript được gọi bằng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

010, nó sẽ sử dụng thông tin đó để phát hiện cách ít tốn kém nhất để kiểm tra loại và phát ra các thay đổi cho dự án của bạn.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

95

Theo mặc định với các cài đặt này, khi chúng tôi chạy

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

075, TypeScript sẽ tìm một tệp có tên

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

014 trong thư mục đầu ra (

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

077). Nếu

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

078 không tồn tại, thì nó sẽ được tạo ra. Nhưng nếu có,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

075 sẽ cố gắng sử dụng tệp đó để kiểm tra loại tăng dần và cập nhật các tệp đầu ra của chúng tôi.

Các tệp

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

014 này có thể bị xóa một cách an toàn và không có bất kỳ tác động nào đến mã của chúng tôi trong thời gian chạy - chúng hoàn toàn được sử dụng để làm cho các phần tổng hợp nhanh hơn. Chúng tôi cũng có thể đặt tên cho họ bất cứ thứ gì chúng tôi muốn và đặt chúng ở bất cứ đâu chúng tôi muốn sử dụng tùy chọn

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

081.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

96

Dự án tổng hợp

Một phần của mục đích với các dự án tổng hợp (

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98S với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

083 được đặt thành

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

056) là các tham chiếu giữa các dự án khác nhau có thể được xây dựng tăng dần. Như vậy, các dự án tổng hợp sẽ luôn tạo các tệp

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

014.always produce

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

014 files.

ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];086

Khi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

086 được sử dụng, tên tệp thông tin xây dựng sẽ được dựa trên tên tệp đầu ra. Ví dụ, nếu tệp JavaScript đầu ra của chúng tôi là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

088, thì theo cờ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

010, TypeScript sẽ tạo tệp

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

090. Như trên, điều này có thể được kiểm soát với tùy chọn

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

081.

Suy luận loại thứ tự cao hơn từ các hàm chung

TypeScript 3.4 hiện có thể tạo ra các loại chức năng chung khi suy luận từ các hàm chung khác tạo ra các biến loại miễn phí cho các suy luận. Điều này có nghĩa là nhiều mẫu thành phần chức năng hiện hoạt động tốt hơn trong 3,4.

Để có được cụ thể hơn, hãy để Lôi xây dựng một số động lực và xem xét chức năng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

092 sau:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

97

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

092 có hai chức năng khác:

  • ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    64 có một số đối số (loại

    ts

    abstract class Shape {

    abstract getArea(): number;

    }

    // ---cut---

    interface HasArea {

    getArea(): number;

    }

    // Works!

    let Ctor: abstract new () => HasArea = Shape;

    68) và trả về giá trị của loại

    ts

    abstract class Shape {

    abstract getArea(): number;

    }

    // ---cut---

    interface HasArea {

    getArea(): number;

    }

    // Works!

    let Ctor: abstract new () => HasArea = Shape;

    69
  • ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    097 có đối số loại

    ts

    abstract class Shape {

    abstract getArea(): number;

    }

    // ---cut---

    interface HasArea {

    getArea(): number;

    }

    // Works!

    let Ctor: abstract new () => HasArea = Shape;

    69 (loại

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    64 được trả về) và trả về giá trị của loại

    ts

    abstract class Shape {

    abstract getArea(): number;

    }

    // ---cut---

    interface HasArea {

    getArea(): number;

    }

    // Works!

    let Ctor: abstract new () => HasArea = Shape;

    67

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

092 sau đó trả về một hàm cung cấp cho đối số của nó thông qua

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

64 và sau đó

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

097.

Khi gọi chức năng này, TypeScript sẽ cố gắng tìm ra các loại

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

68,

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

69 và

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

67 thông qua một quy trình gọi là suy luận đối số loại. Quá trình suy luận này thường hoạt động khá tốt:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

98

Quá trình suy luận khá đơn giản ở đây vì các loại sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

107 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

108 có thể dễ dàng tham chiếu. Tuy nhiên, trong TypeScript 3.3 trở lên, các hàm chung như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

092 didn hoạt động rất tốt khi thông qua các chức năng chung khác.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

99

Trong các phiên bản cũ hơn, TypeScript sẽ suy ra loại đối tượng trống (

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

110) khi suy ra từ các biến loại khác như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

112.

Trong quá trình suy luận đối số loại trong TypeScript 3.4, để gọi đến hàm chung trả về loại chức năng, TypeScript sẽ, nếu thích hợp, truyền các tham số loại từ các đối số hàm chung lên loại chức năng kết quả.

Nói cách khác, thay vì sản xuất loại

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

00

TypeScript 3.4 tạo ra loại

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

01

Lưu ý rằng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 đã được truyền từ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

114 vào danh sách tham số loại loại kết quả. Điều này có nghĩa là tính tổng quát từ các đối số ____ 1092 đã được bảo tồn và mẫu

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

116 của chúng tôi sẽ chỉ hoạt động!

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

02

Để biết thêm chi tiết, bạn có thể đọc thêm tại thay đổi ban đầu.

Cải tiến cho ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];117 và ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];118

TypeScript 3.4 giúp sử dụng các loại giống như mảng chỉ đọc dễ dàng hơn một chút.

Một cú pháp mới cho ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];117

Loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

117 mô tả

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

19 chỉ có thể được đọc từ đó. Bất kỳ biến nào có tham chiếu đến

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

117 đều có thể thêm, xóa hoặc thay thế bất kỳ phần tử nào của mảng.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

03

Mặc dù nó thực hành tốt để sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

117 trên

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

19 khi không có đột biến, nhưng nó thường là một nỗi đau cho rằng các mảng có cú pháp đẹp hơn. Cụ thể,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

125 là phiên bản tốc ký của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

126, giống như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

127 là tốc ký cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

128.

TypeScript 3.4 Giới thiệu một cú pháp mới cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

117 bằng cách sử dụng công cụ sửa đổi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118 mới cho các loại mảng.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

04

ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];118 Bộ dữ liệu

TypeScript 3.4 cũng giới thiệu hỗ trợ mới cho các bộ dữ liệu

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118. Chúng tôi có thể tiền tố bất kỳ loại tuple nào với từ khóa

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118 để biến nó thành một tuple

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118, giống như bây giờ chúng ta có thể với cú pháp tốc ký của Array. Như bạn có thể mong đợi, không giống như các bộ dữ liệu thông thường có các khe cắm có thể được ghi vào,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118 Tuples chỉ cho phép đọc từ các vị trí đó.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

05

Theo cách tương tự mà các bộ dữ liệu thông thường là các loại kéo dài từ

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

19 - một tuple với các yếu tố của loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

138,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

140,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

140 | Tiết ____ ____1054

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

142

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

61 -

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118 Các loại là các loại mở rộng từ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

117. Vì vậy, một bộ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118 với các yếu tố

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

138,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

140, ____ ____1054

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

142 mở rộng từ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

160

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

138 |

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

140 | Mạnh ____ ____1054

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

142

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

61.

ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];118 Các bộ sửa đổi loại được ánh xạ và mảng ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];118

Trong các phiên bản trước của TypeScript, chúng tôi đã tổng quát các loại được ánh xạ để hoạt động khác nhau trên các loại giống như mảng. Điều này có nghĩa là một loại được ánh xạ như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

170 có thể hoạt động trên các mảng và bộ dữ liệu giống nhau.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

06

Thật không may, các loại được ánh xạ như loại tiện ích

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

171 thực sự là không có các loại mảng và tuple.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

07

Trong TypeScript 3.4, công cụ sửa đổi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118 theo loại được ánh xạ sẽ tự động chuyển đổi các loại giống như mảng thành các đối tác

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118 tương ứng của chúng.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

08

Tương tự, bạn có thể viết một loại tiện ích như loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

174 được ánh xạ loại bỏ ____ 1118-ness và điều đó sẽ chuyển đổi các thùng chứa mảng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118 trở lại tương đương thay đổi của chúng.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

09

Hãy cẩn thận

Mặc dù có sự xuất hiện của nó, công cụ sửa đổi loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118 chỉ có thể được sử dụng cho cú pháp trên các loại mảng và loại tuple. Nó không phải là một toán tử loại đa năng.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

10

Bạn có thể xem thêm chi tiết trong yêu cầu kéo.

ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];178 Khẳng định

TypeScript 3.4 giới thiệu một cấu trúc mới cho các giá trị theo nghĩa đen được gọi là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

178. Cú pháp của nó là một khẳng định loại với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

178 thay cho tên loại (ví dụ:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

181). Khi chúng ta xây dựng các biểu thức theo nghĩa đen mới với các xác nhận

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

178, chúng ta có thể báo hiệu cho ngôn ngữ đó

  • Không có loại nghĩa đen nào trong biểu thức đó nên được mở rộng (ví dụ: không đi từ

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    183 đến

    ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    79)
  • đối tượng có nghĩa đen nhận được

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    118 thuộc tính
  • Các chữ viết rẽ trở thành

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    118

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

11

Ngoài các tệp

sh

tsc --explainFiles

86, cú pháp khẳng định khung góc cũng có thể được sử dụng.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

12

Tính năng này có nghĩa là các loại sẽ được sử dụng chỉ để gợi ý tính bất khả xâm phạm đối với trình biên dịch thường có thể được bỏ qua.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

13

Lưu ý những điều trên không cần chú thích loại. Khẳng định

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

178 cho phép TypeScript lấy loại biểu thức cụ thể nhất.

Điều này thậm chí có thể được sử dụng để kích hoạt các mẫu giống như ____ 1189 trong mã JavaScript đơn giản nếu bạn chọn không sử dụng cấu trúc TypeScript.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

14

Hãy cẩn thận

Một điều cần lưu ý là các xác nhận

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

178 chỉ có thể được áp dụng ngay lập tức trên các biểu thức theo nghĩa đen đơn giản.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

15

Một điều khác cần lưu ý là bối cảnh

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

178 don don ngay lập tức chuyển đổi một biểu thức thành hoàn toàn bất biến.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

16

Để biết thêm chi tiết, bạn có thể kiểm tra yêu cầu kéo tương ứng.

Kiểm tra loại cho ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];193

TypeScript 3.4 Giới thiệu hỗ trợ cho Ecmascript kiểm tra loại mới

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

193 - một biến toàn cầu, tốt, đề cập đến phạm vi toàn cầu. Không giống như các giải pháp trên,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

193 cung cấp một cách tiêu chuẩn để truy cập vào phạm vi toàn cầu có thể được sử dụng trên các môi trường khác nhau.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

17

Lưu ý rằng các biến toàn cầu được khai báo với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

196 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

178 don lồng hiển thị trên

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

193.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

18

Nó cũng rất quan trọng cần lưu ý rằng TypeScript không chuyển đổi các tham chiếu thành

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

193 khi biên dịch thành các phiên bản cũ hơn của Ecmascript. Do đó, trừ khi bạn nhắm mục tiêu các trình duyệt thường xanh (đã hỗ trợ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

193), bạn có thể muốn sử dụng một polyfill thích hợp thay thế.

Để biết thêm chi tiết về việc triển khai, hãy xem yêu cầu kéo của tính năng.

TypeScript 3.3

Cải thiện hành vi để gọi các loại công đoàn

Trong các phiên bản trước của TypeScript, các công đoàn có thể gọi chỉ có thể được gọi nếu chúng có danh sách tham số giống hệt nhau.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

19

Tuy nhiên, trong ví dụ trên, cả

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

201S và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

202 sẽ có thể lấy chuỗi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

203 và trả về

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

06 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79.

Trong TypeScript 3.3, đây không còn là lỗi.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

20

Trong TypeScript 3.3, các tham số của các chữ ký này được giao nhau với nhau để tạo ra một chữ ký mới.

Trong ví dụ trên, các tham số

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

206 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

207 được giao với nhau với một tham số mới của loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

208.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

208 thực sự giống như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

210 tương đương với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

211. Mỗi giao điểm không thể đó giảm xuống còn

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13 và chúng tôi đã rời đi với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

213 chỉ là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

203.

Hãy cẩn thận

Một điều cần lưu ý là các xác nhận

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

178 chỉ có thể được áp dụng ngay lập tức trên các biểu thức theo nghĩa đen đơn giản.

Một điều khác cần lưu ý là bối cảnh

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

178 don don ngay lập tức chuyển đổi một biểu thức thành hoàn toàn bất biến.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

21

Để biết thêm chi tiết, bạn có thể kiểm tra yêu cầu kéo tương ứng.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

22

Kiểm tra loại cho ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];193

TypeScript 3.4 Giới thiệu hỗ trợ cho Ecmascript kiểm tra loại mới

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

193 - một biến toàn cầu, tốt, đề cập đến phạm vi toàn cầu. Không giống như các giải pháp trên,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

193 cung cấp một cách tiêu chuẩn để truy cập vào phạm vi toàn cầu có thể được sử dụng trên các môi trường khác nhau.

Lưu ý rằng các biến toàn cầu được khai báo với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

196 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

178 don lồng hiển thị trên

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

193.

Nó cũng rất quan trọng cần lưu ý rằng TypeScript không chuyển đổi các tham chiếu thành

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

193 khi biên dịch thành các phiên bản cũ hơn của Ecmascript. Do đó, trừ khi bạn nhắm mục tiêu các trình duyệt thường xanh (đã hỗ trợ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

193), bạn có thể muốn sử dụng một polyfill thích hợp thay thế.

Để biết thêm chi tiết về việc triển khai, hãy xem yêu cầu kéo của tính năng.a reduction of 50% to 75% in build times of the original

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

219 times. You can read more on the original pull request for the change to see specific numbers, but we believe most composite project users will see significant wins here.

TypeScript 3.3

ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];228

Cải thiện hành vi để gọi các loại công đoàn

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

23

Điều này đạt được bằng cách giới thiệu hai loại mới,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

234 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

235, trong

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

43. Các loại này chứa các khai báo phương pháp chung chuyên dụng cho

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

69,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

232 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

233 cho các hàm và hàm xây dựng thông thường, tương ứng. Các khai báo sử dụng các tham số REST chung chung (xem #24897) để nắm bắt và phản ánh danh sách tham số theo cách được đánh máy mạnh mẽ. Trong chế độ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

228, các khai báo này được sử dụng thay cho các khai báo (rất cho phép) được cung cấp bởi loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

241.

Hãy cẩn thận

Vì các kiểm tra chặt chẽ hơn có thể phát hiện ra các lỗi chưa được báo cáo trước đó, đây là một thay đổi phá vỡ ở chế độ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

30.

Ngoài ra, một cảnh báo khác về chức năng mới này là do những hạn chế nhất định,

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

69,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

232 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

233 có thể mô hình đầy đủ các hàm hoặc chức năng chung có quá tải. Khi sử dụng các phương thức này trên hàm chung, các tham số loại sẽ được thay thế bằng loại đối tượng trống (

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

110) và khi được sử dụng trên một hàm có quá tải, chỉ có quá tải cuối cùng sẽ được mô hình hóa.

Biểu thức lan truyền chung trong các chữ cái đối tượng

Trong TypeScript 3.2, các chữ cái đối tượng hiện cho phép các biểu thức lan truyền chung hiện đang tạo ra các loại giao điểm, tương tự như hàm

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

247 và chữ JSX. Ví dụ:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

24

Bài tập thuộc tính và các biểu thức lan truyền không chung được hợp nhất ở mức độ lớn nhất có thể ở hai bên của biểu thức lan truyền chung. Ví dụ:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

25

Các biểu thức lan truyền không thể hiện được tiếp tục được xử lý như trước đây: các chữ ký gọi và xây dựng bị tước, chỉ các thuộc tính không phương pháp được bảo tồn và đối với các thuộc tính có cùng tên, loại thuộc tính ngoài cùng bên phải được sử dụng. Điều này tương phản với các loại giao điểm kết nối các chữ ký gọi và xây dựng chữ ký, bảo tồn tất cả các thuộc tính và giao nhau các loại thuộc tính có cùng tên. Do đó, sự lây lan của cùng một loại có thể tạo ra kết quả khác nhau khi chúng được tạo ra thông qua việc khởi tạo các loại chung:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

26

Các biến và tham số phần còn lại đối tượng chung

TypeScript 3.2 cũng cho phép phá hủy một ràng buộc nghỉ ngơi từ một biến chung. Điều này đạt được bằng cách sử dụng các loại trợ giúp

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

248 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

249 được xác định trước từ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

43 và sử dụng loại chung trong câu hỏi cũng như tên của các ràng buộc khác trong mô hình phá hủy.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

27

Bigint

Bigint là một phần của đề xuất sắp tới trong Ecmascript cho phép chúng tôi mô hình hóa các số nguyên lớn theo lý thuyết. TypeScript 3.2 mang đến việc kiểm tra loại cho Bigint, cũng như hỗ trợ phát ra các chữ Bigint khi nhắm mục tiêu

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

59.

Hỗ trợ Bigint trong TypeScript giới thiệu một loại nguyên thủy mới được gọi là

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

69 (tất cả chữ thường). Bạn có thể nhận được

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

69 bằng cách gọi hàm

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

254 hoặc bằng cách viết ra một chữ lớn bằng cách thêm

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

142 vào cuối bất kỳ số nguyên số nguyên:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

28

Trong khi bạn có thể tưởng tượng sự tương tác gần giữa

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

06 và

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

69, cả hai là các miền riêng biệt.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

29

Như được chỉ định trong ECMAScript, trộn

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

06S và

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

69 trong các hoạt động số học là một lỗi. Bạn phải chuyển đổi rõ ràng các giá trị thành

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

260s.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

30

Cũng quan trọng cần lưu ý là

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

69S tạo ra một chuỗi mới khi sử dụng toán tử

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

262: Chuỗi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

263. Do đó, TypeScript thu hẹp chính xác bằng cách sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

262 như bạn mong đợi.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

31

Chúng tôi muốn mở rộng một lời cảm ơn rất lớn đến Caleb Sander cho tất cả các công việc về tính năng này. Chúng tôi rất biết ơn về sự đóng góp, và chúng tôi chắc chắn rằng người dùng của chúng tôi cũng vậy!

Hãy cẩn thận

Như chúng tôi đã đề cập, hỗ trợ Bigint chỉ có sẵn cho mục tiêu

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

59. Có thể không rõ ràng, nhưng bởi vì Bigint có hành vi khác nhau đối với các toán tử toán học như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

266,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

267,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

268, v.v., việc cung cấp chức năng cho các mục tiêu cũ hơn trong đó tính năng không tồn tại . TypeScript sẽ cần phải gửi đến hành vi chính xác tùy thuộc vào loại, và do đó, mỗi lần bổ sung, nối chuỗi, nhân, v.v. sẽ liên quan đến một cuộc gọi chức năng.

Vì lý do đó, chúng tôi không có kế hoạch ngay lập tức để cung cấp hỗ trợ hạ thấp. Về mặt sáng, nút 11 và các phiên bản Chrome mới hơn đã hỗ trợ tính năng này, do đó, bạn sẽ có thể sử dụng Bigint ở đó khi nhắm mục tiêu

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

59.

Một số mục tiêu nhất định có thể bao gồm một đối tượng thời gian chạy polyfill hoặc lớn như Bigint. Đối với các mục đích đó, bạn có thể muốn thêm

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

271 vào cài đặt

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

272 trong các tùy chọn trình biên dịch của bạn.

Các loại không đơn vị là phân biệt đối xử liên minh

TypeScript 3.2 giúp thu hẹp dễ dàng hơn bằng cách thư giãn các quy tắc cho những gì nó coi là một tài sản phân biệt đối xử. Các thuộc tính chung của các công đoàn hiện được coi là phân biệt đối xử miễn là chúng chứa một số loại singleton (ví dụ: một chuỗi theo nghĩa đen,

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57) và chúng không chứa thuốc generic.

Do đó, TypeScript 3.2 xem xét thuộc tính

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

12 trong ví dụ sau đây là một sự phân biệt đối xử, trong khi trước đó nó sẽ không phải là một loại singleton. Nhờ điều này, việc thu hẹp hoạt động chính xác trong phần thân của chức năng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

277.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

32

tsinterface Options { /** File patterns to be excluded. */ exclude?: string[]; /** * It handles any extra properties that we haven't declared as type 'any'. */ [x: string]: any;}function processOptions(opts: Options) { // Notice we're *intentionally* accessing `excludes`, not `exclude` if (opts.excludes) { console.error( "The option `excludes` is not valid. Did you mean `exclude`?" ); }}98 Kế thừa qua các gói Node.js

TypeScript 3.2 hiện đã giải quyết

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98S từ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

44. Khi sử dụng đường dẫn trần cho trường

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

281 trong

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98, TypeScript sẽ đi sâu vào các gói

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

44 cho chúng tôi.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

33

Tại đây, TypeScript sẽ leo lên các thư mục

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

44 đang tìm kiếm gói

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

285. Đối với mỗi gói đó, trước tiên, TypeScript sẽ kiểm tra xem

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

286 có chứa trường

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

287 hay không, và nếu có, TypeScript sẽ cố gắng tải tệp cấu hình từ trường đó. Nếu không tồn tại, TypeScript sẽ cố gắng đọc từ

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98 tại gốc. Điều này tương tự như quy trình tra cứu cho các tệp

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

20 trong các gói mà Node sử dụng và quy trình tra cứu

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 mà TypeScript đã sử dụng.

Tính năng này có thể cực kỳ hữu ích cho các tổ chức lớn hơn hoặc các dự án có nhiều phụ thuộc phân tán.

Cờ ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];291 mới

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

075, Trình biên dịch TypeScript, hỗ trợ một lá cờ mới có tên là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

291. Khi chạy

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

294, TypeScript sẽ tính toán

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98 hiệu quả (sau khi tính toán các tùy chọn được kế thừa từ trường

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

281) và in ra. Điều này có thể hữu ích để chẩn đoán các vấn đề cấu hình nói chung.

sh# Forward output to a text filetsc --explainFiles > expanation.txt# Pipe output to a utility program like `less`, or an editor like VS Codetsc --explainFiles | lesstsc --explainFiles | code -03 Tuyên bố trong JavaScript

Khi viết trong các tệp JavaScript (sử dụng

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

45), TypeScript hiện nhận ra các khai báo sử dụng

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

03. Điều này có nghĩa là bạn sẽ hoàn thành tốt hơn và kiểm tra loại mạnh hơn khi bật kiểm tra loại trong các tệp JavaScript (bằng cách bật tùy chọn

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

44 hoặc thêm nhận xét

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

74 vào đầu tệp của bạn).

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

34

TypeScript 3.1

Các loại được ánh xạ trên các bộ đếm và mảng

Trong TypeScript 3.1, các loại đối tượng được ánh xạ [1] trên các bộ dữ liệu và mảng hiện tạo ra các bộ dữ liệu/mảng mới, thay vì tạo một loại mới trong đó các thành viên như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

302,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

303 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

304 được chuyển đổi. Ví dụ:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

35

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

305 có loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 và khi loại đó là một tuple như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

307, chỉ có các thuộc tính số được chuyển đổi. Trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

308, có hai thuộc tính được đặt tên bằng số:

sh

tsc --explainFiles

21 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

138. Khi được cung cấp một tuple như thế,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

305 sẽ tạo ra một bộ phim mới trong đó các thuộc tính

sh

tsc --explainFiles

21 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

138 là

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

16 của loại ban đầu. Vì vậy, loại kết quả

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

315 kết thúc với loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

316.

Tuyên bố thuộc tính trên các chức năng

TypeScript 3.1 mang đến khả năng xác định các thuộc tính trên các khai báo chức năng và các funcs ____ 1178 được tuyên bố, chỉ bằng cách gán cho các thuộc tính trên các chức năng này trong cùng một phạm vi. Điều này cho phép chúng tôi viết mã JavaScript chính tắc mà không cần dùng đến các bản hack

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

004. Ví dụ:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

36

Ở đây, chúng ta có một hàm

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

319 đọc một hình ảnh theo cách không đồng bộ không chặn. Ngoài

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

319, chúng tôi đã cung cấp một chức năng thuận tiện trên

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

319 được gọi là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

322.

Mặc dù xuất khẩu Ecmascript thường là một cách tốt hơn để cung cấp chức năng này, nhưng hỗ trợ mới này cho phép mã được viết theo kiểu này để chỉ hoạt động. Ngoài ra, cách tiếp cận này để khai báo tài sản cho phép chúng tôi thể hiện các mẫu phổ biến như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

323 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

324 trên các thành phần chức năng không trạng thái của React (SFC).

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

37

[1] Cụ thể hơn, các loại ánh xạ đồng hình như ở dạng trên.

Lựa chọn phiên bản với ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];325

Phản hồi từ cộng đồng của chúng tôi, cũng như kinh nghiệm của chính chúng tôi, đã cho chúng tôi thấy rằng việc tận dụng các tính năng TypeScript mới nhất trong khi cũng có liên quan đến người dùng trên các phiên bản cũ hơn là khó khăn. TypeScript giới thiệu một tính năng mới có tên

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

325 để giúp chứa các kịch bản này.

Khi sử dụng độ phân giải mô -đun nút trong TypeScript 3.1, khi TypeScript sẽ mở tệp

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

286 để tìm ra tệp nào cần đọc, trước tiên nó xem xét một trường mới có tên

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

325. A

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

286 với trường

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

325 có thể trông như thế này:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

38

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

286 này cho TypeScript này kiểm tra xem phiên bản hiện tại của TypeScript có chạy hay không. Nếu nó có từ 3.1 trở lên, nó sẽ tìm ra đường dẫn mà bạn đã nhập liên quan đến gói và đọc từ thư mục gói

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

332. Đó là những gì mà

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

333 có nghĩa là - nếu bạn quen thuộc với bản đồ đường dẫn ngày hôm nay, nó hoạt động chính xác như thế.

Vì vậy, trong ví dụ trên, nếu chúng tôi nhập từ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

334, chúng tôi sẽ cố gắng giải quyết từ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

335 (và các đường dẫn có liên quan khác) khi chạy trong TypeScript 3.1. Nếu chúng tôi nhập từ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

336, chúng tôi sẽ cố gắng tìm kiếm

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

337 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

338.

Điều gì sẽ xảy ra nếu chúng ta không chạy trong TypeScript 3.1 trong ví dụ này? Chà, nếu không có trường nào trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

325 được khớp, TypeScript sẽ quay trở lại trường

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

340, do đó, ở đây TypeScript 3.0 và trước đó sẽ được chuyển hướng đến

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

341.

Hành vi phù hợp

Cách mà TypeScript quyết định xem một phiên bản của trình biên dịch & ngôn ngữ có khớp hay không bằng cách sử dụng các phạm vi Semver Node Node.

Nhiều trường

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

325 có thể hỗ trợ nhiều trường trong đó mỗi tên trường được chỉ định bởi phạm vi để khớp.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

39

Vì phạm vi có khả năng trùng lặp, việc xác định chuyển hướng áp dụng là cụ thể theo thứ tự. Điều đó có nghĩa là trong ví dụ trên, mặc dù cả

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

343 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

344 đều hỗ trợ TypeScript 3.2 trở lên, việc đảo ngược thứ tự có thể có hành vi khác nhau, do đó, mẫu trên sẽ không tương đương với sau.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

40

TypeScript 3.0

Tuples trong các tham số nghỉ ngơi và biểu thức lan truyền

TypeScript 3.0 thêm hỗ trợ cho nhiều khả năng mới để tương tác với danh sách tham số chức năng dưới dạng các loại Tuple. TypeScript 3.0 Thêm hỗ trợ cho:

  • Mở rộng các tham số REST với các loại tuple thành các tham số riêng biệt.
  • Mở rộng các biểu thức lan truyền với các loại tuple thành các đối số riêng biệt.
  • Tham số phần còn lại chung và suy luận tương ứng của các loại tuple.
  • Các yếu tố tùy chọn trong các loại tuple.
  • Các yếu tố nghỉ trong các loại tuple.

Với các tính năng này, có thể gõ mạnh một số hàm bậc cao hơn để biến đổi các hàm và danh sách tham số của chúng.

Các tham số nghỉ ngơi với các loại Tuple

Khi một tham số REST có loại tuple, loại tuple được mở rộng thành một chuỗi các tham số riêng biệt. Ví dụ: hai tuyên bố sau đây là tương đương:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

41

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

42

Truyền biểu cảm với các loại tuple

Khi một cuộc gọi hàm bao gồm một biểu thức lan truyền của một loại tuple là đối số cuối cùng, biểu thức lan truyền tương ứng với một chuỗi các đối số riêng biệt của các loại phần tử tuple.

Do đó, các cuộc gọi sau là tương đương:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

43

Tham số phần còn lại chung

Một tham số REST được phép có một loại chung bị ràng buộc với loại mảng và loại suy luận có thể suy ra các loại tuple cho các tham số REST chung như vậy. Điều này cho phép chụp và lan truyền các danh sách tham số một phần cao hơn:

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

44

Trong Tuyên bố

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

345 ở trên, loại suy luận loại Infer loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

06,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

347 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

348 cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

112 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

351.

Lưu ý rằng khi một loại tuple được suy ra từ một chuỗi các tham số và sau đó được mở rộng thành một danh sách tham số, như trường hợp của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

112, các tên tham số ban đầu được sử dụng trong bản mở rộng (tuy nhiên, các tên không có ý nghĩa ngữ nghĩa và không phải là có thể quan sát được).

Các yếu tố tùy chọn trong các loại tuple

Các loại Tuple hiện cho phép một postfix

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

353 trên các loại phần tử để chỉ ra rằng phần tử là tùy chọn:

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

45

Trong Tuyên bố

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

345 ở trên, loại suy luận loại Infer loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

06,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

347 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

348 cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

112 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

351.

Lưu ý rằng khi một loại tuple được suy ra từ một chuỗi các tham số và sau đó được mở rộng thành một danh sách tham số, như trường hợp của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

112, các tên tham số ban đầu được sử dụng trong bản mở rộng (tuy nhiên, các tên không có ý nghĩa ngữ nghĩa và không phải là có thể quan sát được).

Các yếu tố tùy chọn trong các loại tuple

Các loại Tuple hiện cho phép một postfix

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

353 trên các loại phần tử để chỉ ra rằng phần tử là tùy chọn:

Trong chế độ tsinterface SomeType { /** This is an index signature. */ [propName: string]: any;}function doStuff(value: SomeType) { let x = value["someProperty"];}51, công cụ sửa đổi ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];353 tự động bao gồm tsinterface SomeType { /** This is an index signature. */ [propName: string]: any;}function doStuff(value: SomeType) { let x = value["someProperty"];}57 trong loại phần tử, tương tự như các tham số tùy chọn.

Loại tuple cho phép một phần tử bị bỏ qua nếu nó có công cụ sửa đổi postfix

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

353 ở loại của nó và tất cả các phần tử ở bên phải của nó cũng có trình sửa đổi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

353.

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

46

Trong Tuyên bố

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

345 ở trên, loại suy luận loại Infer loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

06,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

347 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

348 cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

112 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

351.

Lưu ý rằng khi một loại tuple được suy ra từ một chuỗi các tham số và sau đó được mở rộng thành một danh sách tham số, như trường hợp của ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];112, các tên tham số ban đầu được sử dụng trong bản mở rộng (tuy nhiên, các tên không có ý nghĩa ngữ nghĩa và không phải là có thể quan sát được).

Các yếu tố tùy chọn trong các loại tuple

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

47

Hỗ trợ cho ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];323 trong JSX

TypeScript 2.9 và trước đó đã không có đòn bẩy phản ứng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

323 khai báo bên trong các thành phần JSX. Người dùng thường phải khai báo các thuộc tính tùy chọn và sử dụng các xác nhận không có null bên trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

380 hoặc họ sử dụng các tổ chức loại để khắc phục loại thành phần trước khi xuất.

TypeScript 3.0 Thêm hỗ trợ một bí danh loại mới trong không gian tên

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

381 được gọi là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

382. Loại trợ giúp này xác định một phép biến đổi trên thành phần loại ____ ____1383, trước khi sử dụng để kiểm tra biểu thức JSX nhắm mục tiêu vào nó; Do đó, cho phép tùy chỉnh như: Cách xung đột giữa các đạo cụ được cung cấp và các đạo cụ được suy ra được xử lý, cách suy luận được ánh xạ, cách xử lý tính tùy chọn và cách kết hợp từ các nơi khác nhau.

Nói tóm lại bằng cách sử dụng loại chung này, chúng ta có thể mô hình hóa hành vi cụ thể của React React đối với những thứ như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

323 và, ở một mức độ nào đó,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

324.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

48

Hãy cẩn thận

Các loại rõ ràng trên

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

323

Các thuộc tính mặc định được suy ra từ loại thuộc tính

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

323. Nếu một chú thích loại rõ ràng được thêm vào, ví dụ:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

388 Trình biên dịch sẽ không thể xác định các thuộc tính nào có mặc định (vì loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

323 bao gồm tất cả các thuộc tính của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

383).

Thay vào đó, sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

391 như một chú thích loại rõ ràng hoặc không thêm chú thích loại như được thực hiện trong ví dụ trên.

Đối với các thành phần chức năng không trạng thái (SFC), hãy sử dụng bộ khởi tạo mặc định ES2015 cho SFCS:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

49
Thay đổi thành

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

392

Các thay đổi tương ứng để thêm định nghĩa

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

382 vào không gian tên

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

381 trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

392 vẫn cần thiết. Hãy nhớ rằng có một số hạn chế.

ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];396 Chỉ thị tham khảo

TypeScript thêm một chỉ thị tham chiếu ba lần (

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

397) mới, cho phép một tệp để bao gồm một tệp LIB tích hợp hiện có.

Các tệp LIB tích hợp được tham chiếu theo cùng kiểu với tùy chọn trình biên dịch

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

272 trong tsconfig.json (ví dụ: sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

399 và không phải

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

400, v.v.).

Đối với các tác giả tập tin khai báo dựa vào các loại tích hợp, ví dụ: DOM API hoặc các nhà xây dựng thời gian chạy JS tích hợp như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

401 hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

402, các chỉ thị LIB tham chiếu ba lần là được khuyến nghị. Trước đây, các tệp .d.ts này đã phải thêm các khai báo chuyển tiếp/trùng lặp của các loại đó.

Thí dụ

Sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

403 cho một trong các tệp trong phần biên dịch tương đương với việc biên dịch với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

404.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

50

TypeScript 2.9

Hỗ trợ tsinterface Options { /** File patterns to be excluded. */ exclude?: string[]; /** * It handles any extra properties that we haven't declared as type 'any'. */ [x: string]: any;}function processOptions(opts: Options) { // Notice we're *intentionally* accessing `excludes`, not `exclude` if (opts.excludes) { console.error( "The option `excludes` is not valid. Did you mean `exclude`?" ); }}06 và ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];406 thuộc tính được đặt tên với ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];407 và các loại được ánh xạ

TypeScript 2.9 bổ sung hỗ trợ cho

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

06 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

406 thuộc tính được đặt tên trong các loại chỉ mục và các loại được ánh xạ. Trước đây, toán tử

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

407 và các loại được ánh xạ chỉ được hỗ trợ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79 thuộc tính được đặt tên.

Thay đổi bao gồm:

  • Loại chỉ mục

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    412 cho một số loại

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    054 là một kiểu con của

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    414.
  • Một loại được ánh xạ

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    415 cho phép bất kỳ

    ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    97 có thể gán cho

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    414.
  • Trong một câu lệnh

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    418 cho một đối tượng thuộc loại chung

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    054, loại được suy ra của biến lặp trước đây là

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    412 nhưng hiện là

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    421. (Nói cách khác, tập hợp con của

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    412 chỉ bao gồm các giá trị giống như chuỗi.)

Cho một loại đối tượng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

364,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

424 được giải quyết như sau:

  • Nếu

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    364 chứa chữ ký chỉ mục chuỗi,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    424 là một liên kết của

    ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    79,

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    06 và các loại nghĩa đen đại diện cho các thuộc tính giống như biểu tượng, nếu không
  • Nếu

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    364 chứa chữ ký chỉ số số,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    424 là một liên kết của

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    06 và các loại nghĩa đen đại diện cho các thuộc tính giống như chuỗi và giống như biểu tượng, nếu không
  • ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    424 là một liên minh của các loại nghĩa đen đại diện cho các thuộc tính giống như chuỗi, giống như số và giống như biểu tượng.

Where:

  • Các thuộc tính giống như chuỗi của một loại đối tượng là các thuộc tính được khai báo bằng định danh, một chuỗi theo nghĩa đen hoặc một tên thuộc tính được tính toán của một loại theo nghĩa đen.
  • Các thuộc tính giống như số của một loại đối tượng là các thuộc tính được khai báo bằng cách sử dụng tên thuộc tính bằng chữ hoặc tính toán của một loại chữ số.
  • Các thuộc tính giống như biểu tượng của một loại đối tượng là các thuộc tính được khai báo bằng cách sử dụng tên thuộc tính được tính toán của một loại ký hiệu duy nhất.

Trong một loại được ánh xạ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

415, mỗi loại chuỗi theo nghĩa đen trong

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

97 giới thiệu một thuộc tính có tên chuỗi, mỗi loại chữ số trong

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

97 giới thiệu một thuộc tính có tên số và mỗi loại biểu tượng duy nhất trong

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

97 giới thiệu một thuộc tính có tên biểu tượng duy nhất. Hơn nữa, nếu

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

97 bao gồm loại

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79, chữ ký chỉ mục chuỗi được giới thiệu và nếu

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

97 bao gồm loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

06, một chữ ký chỉ số số được giới thiệu.

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

51

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

407 hiện phản ánh sự hiện diện của chữ ký chỉ số số bằng cách bao gồm loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

06 trong loại khóa, các loại được ánh xạ như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

443 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

444 hoạt động chính xác khi áp dụng cho các loại đối tượng có chữ ký chỉ số số:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

52

Hơn nữa, với hỗ trợ của nhà điều hành

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

407 cho các khóa

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

06 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

406, giờ đây có thể trừu tượng hóa quyền truy cập vào các thuộc tính của các đối tượng được lập chỉ mục bởi các chữ số (như các loại enum số) và các biểu tượng duy nhất.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

53

Đây là một sự thay đổi phá vỡ; Trước đây, toán tử

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

407 và các loại được ánh xạ chỉ được hỗ trợ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79 thuộc tính được đặt tên. Mã giả định các giá trị được gõ bằng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

412 luôn là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79, giờ đây sẽ được gắn cờ là lỗi.

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

54

khuyến nghị

  • Nếu các chức năng của bạn chỉ có thể xử lý các khóa thuộc tính có tên chuỗi, hãy sử dụng

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    421 trong khai báo:

    ts

    let foo: [...string[], number];

    foo = [123];

    foo = ["hello", 123];

    foo = ["hello!", "hello!", "hello!", 123];

    let bar: [boolean, ...string[], boolean];

    bar = [true, false];

    bar = [true, "some text", false];

    bar = [true, "some", "separated", "text", false];

    55
  • Nếu các chức năng của bạn được mở để xử lý tất cả các khóa thuộc tính, thì các thay đổi sẽ được thực hiện xuống dòng:

    ts

    let foo: [...string[], number];

    foo = [123];

    foo = ["hello", 123];

    foo = ["hello!", "hello!", "hello!", 123];

    let bar: [boolean, ...string[], boolean];

    bar = [true, false];

    bar = [true, "some text", false];

    bar = [true, "some", "separated", "text", false];

    56
  • Nếu không, hãy sử dụng tùy chọn trình biên dịch

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    453 để vô hiệu hóa hành vi mới.

Đối số loại chung trong các phần tử JSX

Các phần tử JSX hiện cho phép chuyển các đối số loại cho các thành phần chung.

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

57

Đối số loại chung trong các mẫu được gắn thẻ chung

Các mẫu được gắn thẻ là một hình thức gọi được giới thiệu trong Ecmascript 2015. Giống như các biểu thức cuộc gọi, các hàm chung có thể được sử dụng trong một mẫu được gắn thẻ và TypeScript sẽ suy ra các đối số loại được sử dụng.

TypeScript 2.9 cho phép chuyển các đối số loại chung cho chuỗi mẫu được gắn thẻ.

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

58

tsabstract class SuperClass { abstract someMethod(): void; badda() {}}type AbstractConstructor = abstract new (...args: any[]) => Tfunction withStyles>(Ctor: T) { abstract class StyledClass extends Ctor { getStyles() { // ... } } return StyledClass;}class SubClass extends withStyles(SuperClass) { someMethod() { this.someMethod() }}51 Các loại

Các mô -đun có thể nhập các loại được khai báo trong các mô -đun khác. Nhưng các tập lệnh toàn cầu không mô-đun không thể truy cập các loại được khai báo trong các mô-đun. Nhập các loại

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

51.

Sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

456 trong một chú thích loại cho phép tiếp cận trong một mô -đun và truy cập khai báo đã xuất mà không cần nhập.

Thí dụ

Đưa ra tuyên bố của lớp

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

457 trong tệp mô -đun:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

59

Có thể được sử dụng trong tệp không mô-đun

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

458:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

60

Điều này cũng hoạt động trong các nhận xét của JSDOC để chỉ các loại từ các mô -đun khác trong

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

20:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

61

Tuyên bố thư giãn phát ra các quy tắc trực quan

Với các loại

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

51 có sẵn, nhiều lỗi về khả năng hiển thị được báo cáo trong quá trình tạo tệp khai báo có thể được xử lý bởi trình biên dịch mà không cần phải thay đổi đầu vào.

Ví dụ:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

62

Với TypeScript 2.9, không có lỗi nào được báo cáo và bây giờ tệp được tạo trông giống như:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

63

TypeScript 2.9 giới thiệu hỗ trợ cho

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

88, một tài sản meta mới như được mô tả bởi đề xuất TC39 hiện tại.

Loại

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

88 là loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

463 toàn cầu được xác định trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

464. Giao diện này là vô cùng hạn chế. Việc thêm các thuộc tính nổi tiếng cho nút hoặc trình duyệt yêu cầu hợp nhất giao diện và có thể tăng cường toàn cầu tùy thuộc vào bối cảnh.

Thí dụ

Giả sử rằng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

465 luôn có sẵn trên

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

88, tuyên bố sẽ được thực hiện thông qua việc mở lại giao diện

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

463:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

64

Và việc sử dụng sẽ là:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

65

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

88 chỉ được phép khi nhắm mục tiêu

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

469 Các mục tiêu và mục tiêu ECMAScript.

Mới ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];470

Thông thường trong các ứng dụng Node.js A

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

471 là cần thiết. Với TypeScript 2.9,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

472 cho phép nhập, trích xuất các loại từ và tạo các tệp

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

471.

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

66

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

67

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

68

ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];474 đầu ra theo mặc định

Bắt đầu TypeScript 2.9 Lỗi được hiển thị theo

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

475 theo mặc định nếu thiết bị đầu ra được áp dụng cho văn bản đầy màu sắc. TypeScript sẽ kiểm tra xem STEAM đầu ra có đặt thuộc tính

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

476 không.

Sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

477 trên dòng lệnh hoặc đặt

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

478 trong

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98 của bạn để vô hiệu hóa đầu ra

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

475.

Mới ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];481

Kích hoạt

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

482 cùng với

sh

tsc --explainFiles

83 khiến trình biên dịch phát ra các tệp

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

484 cùng với các tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 đầu ra. Các dịch vụ ngôn ngữ hiện cũng có thể hiểu các tệp bản đồ này và sử dụng chúng để lập bản đồ các vị trí định nghĩa dựa trên tệp khai báo cho nguồn gốc của chúng, khi có sẵn.

Nói cách khác, việc nhấn định nghĩa trên một tuyên bố từ tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 được tạo bằng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

482 sẽ đưa bạn đến vị trí tệp nguồn (

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

45) nơi tuyên bố đó được xác định và không phải là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19.

TypeScript 2.8

Các loại có điều kiện

TypeScript 2.8 giới thiệu các loại có điều kiện thêm khả năng thể hiện ánh xạ loại không đồng nhất. Một loại có điều kiện chọn một trong hai loại có thể dựa trên một điều kiện được biểu thị dưới dạng thử nghiệm mối quan hệ loại:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

69

Loại trên có nghĩa là khi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 có thể gán cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

112 Loại là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

364, nếu không loại là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

493.

Một loại có điều kiện

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

494 được giải quyết thành

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

364 hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

493 hoặc hoãn lại vì điều kiện phụ thuộc vào một hoặc nhiều biến loại. Việc giải quyết hay trì hoãn được xác định như sau:

  • Đầu tiên, các loại được đưa ra

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    497 và

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    498 là sự khởi tạo của

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    054 và

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    112 trong đó tất cả các trường hợp xuất hiện của các tham số loại được thay thế bằng

    ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    72, nếu

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    497 không thể gán cho

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    498, loại có điều kiện. Theo trực giác, nếu sự khởi tạo cho phép nhất của

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    054 không thể gán cho việc khởi tạo cho phép nhất của

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    112, chúng ta biết rằng sẽ không có sự khởi tạo nào và chúng ta có thể giải quyết thành

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    493.
  • Tiếp theo, đối với mỗi biến loại được giới thiệu bởi khai báo

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    508 (sau đó) trong

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    112 thu thập một tập hợp các loại ứng cử viên bằng cách suy ra từ

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    054 đến

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    112 (sử dụng thuật toán suy luận tương tự như loại suy luận cho các hàm chung). Đối với một biến loại

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    508 đã cho

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    351, nếu bất kỳ ứng cử viên nào được suy ra từ các vị trí đồng biến đổi, loại được suy ra cho

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    351 là một liên minh của các ứng cử viên đó. Mặt khác, nếu bất kỳ ứng cử viên nào được suy ra từ các vị trí biến đổi, loại được suy ra cho

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    351 là một giao điểm của các ứng cử viên đó. Mặt khác, loại được suy ra cho

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    351 là

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    13.
  • Sau đó, với loại

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    518 là khởi tạo

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    054 trong đó tất cả các biến loại

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    508 được thay thế bằng các loại được suy ra trong bước trước, nếu

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    518 chắc chắn được gán cho

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    112, loại có điều kiện được giải quyết thành

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    364. Mối quan hệ chắc chắn có thể gán là giống như mối quan hệ có thể gán thông thường, ngoại trừ các ràng buộc biến loại đó không được xem xét. Theo trực giác, khi một loại chắc chắn được gán cho một loại khác, chúng tôi biết rằng nó sẽ được gán cho tất cả các loại khởi tạo của các loại đó.
  • Mặt khác, điều kiện phụ thuộc vào một hoặc nhiều biến loại và loại có điều kiện được hoãn lại.

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

70

Các loại có điều kiện phân phối

Các loại có điều kiện trong đó loại được kiểm tra là một tham số loại trần được gọi là các loại có điều kiện phân phối. Các loại có điều kiện phân phối được tự động phân phối qua các loại liên minh trong quá trình khởi tạo. Ví dụ, việc khởi tạo

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

494 với đối số loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

525 cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 được giải quyết là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

527.

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

71

Các loại có điều kiện phân phối

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

72

Các loại có điều kiện phân phối

Các loại có điều kiện trong đó loại được kiểm tra là một tham số loại trần được gọi là các loại có điều kiện phân phối. Các loại có điều kiện phân phối được tự động phân phối qua các loại liên minh trong quá trình khởi tạo. Ví dụ, việc khởi tạo

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

494 với đối số loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

525 cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 được giải quyết là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

527.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

73

Trong các thông tin của loại có điều kiện phân phối

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

494, các tham chiếu đến

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 trong loại có điều kiện được giải quyết cho các thành phần riêng lẻ của loại liên minh (tức là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 đề cập đến các thành phần riêng lẻ sau loại có điều kiện được phân phối theo loại liên kết). Hơn nữa, các tham chiếu đến

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

364 có một ràng buộc tham số loại bổ sung

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

112 (tức là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 được coi là có thể gán cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

112 trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

364).

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

74

Lưu ý rằng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 có ràng buộc bổ sung

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

538 trong nhánh thực của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

539 và do đó có thể đề cập đến loại phần tử của mảng là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

540. Ngoài ra, hãy chú ý cách loại có điều kiện được phân phối qua loại liên minh trong ví dụ cuối cùng.

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

75

Các loại có điều kiện phân phối

Các loại có điều kiện trong đó loại được kiểm tra là một tham số loại trần được gọi là các loại có điều kiện phân phối. Các loại có điều kiện phân phối được tự động phân phối qua các loại liên minh trong quá trình khởi tạo. Ví dụ, việc khởi tạo

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

494 với đối số loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

525 cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 được giải quyết là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

527.

Trong các thông tin của loại có điều kiện phân phối

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

494, các tham chiếu đến

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 trong loại có điều kiện được giải quyết cho các thành phần riêng lẻ của loại liên minh (tức là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 đề cập đến các thành phần riêng lẻ sau loại có điều kiện được phân phối theo loại liên kết). Hơn nữa, các tham chiếu đến

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

364 có một ràng buộc tham số loại bổ sung

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

112 (tức là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 được coi là có thể gán cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

112 trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

364).

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

76

Lưu ý rằng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 có ràng buộc bổ sung

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

538 trong nhánh thực của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

539 và do đó có thể đề cập đến loại phần tử của mảng là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

540. Ngoài ra, hãy chú ý cách loại có điều kiện được phân phối qua loại liên minh trong ví dụ cuối cùng.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

77

Thuộc tính phân phối của các loại có điều kiện có thể được sử dụng một cách thuận tiện để lọc các loại kết hợp:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

78

Các loại có điều kiện đặc biệt hữu ích khi kết hợp với các loại được ánh xạ:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

79

Tương tự như các loại liên minh và giao điểm, các loại có điều kiện không được phép tham khảo bản thân theo cách đệ quy. Ví dụ như sau đây là một lỗi.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

80

Không thể sử dụng các khai báo

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

508 trong các điều khoản ràng buộc cho các tham số loại thông thường:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

81

Tuy nhiên, có thể thu được nhiều hiệu ứng tương tự bằng cách xóa các biến loại trong ràng buộc và thay vào đó chỉ định một loại có điều kiện:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

82

Các loại có điều kiện được xác định trước

TypeScript 2.8 Thêm một số loại có điều kiện được xác định trước vào

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

43:

  • ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    546 - Loại trừ từ

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    054 những loại có thể gán cho

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    112.
  • ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    549 - Trích xuất từ ​​

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    054 những loại có thể gán cho

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    112.
  • ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    552 - Loại trừ

    ts

    abstract class Shape {

    abstract getArea(): number;

    }

    // ---cut---

    interface HasArea {

    getArea(): number;

    }

    // Works!

    let Ctor: abstract new () => HasArea = Shape;

    07 và

    ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    57 từ

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    054.
  • ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    556 - Lấy loại trả về của loại chức năng.
  • ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    557 - Lấy loại thể hiện của loại hàm tạo hàm.

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

83

Lưu ý: Loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

249 là một triển khai thích hợp của loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

559 được đề xuất ở đây. Chúng tôi đã sử dụng tên

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

249 để tránh phá vỡ mã hiện có xác định

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

559, cộng với chúng tôi cảm thấy tên đó truyền tải tốt hơn ngữ nghĩa của loại. Chúng tôi không bao gồm loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

562 vì nó được viết tầm thường là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

563.

Cải thiện kiểm soát các bộ sửa đổi loại được ánh xạ

Hỗ trợ các loại được ánh xạ thêm một công cụ sửa đổi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118 hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

353 vào thuộc tính được ánh xạ, nhưng chúng không cung cấp hỗ trợ khả năng xóa các công cụ sửa đổi. Điều này quan trọng trong các loại được ánh xạ đồng nhất theo mặc định bảo tồn các biến đổi của loại cơ bản.

TypeScript 2.8 Thêm khả năng cho một loại được ánh xạ để thêm hoặc xóa một công cụ sửa đổi cụ thể. Cụ thể, công cụ sửa đổi thuộc tính

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118 hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

353 theo loại được ánh xạ hiện có thể được tiền tố với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

266 hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

267 để chỉ ra rằng cần thêm hoặc loại bỏ công cụ sửa đổi.

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

84

Lưu ý: Loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

249 là một triển khai thích hợp của loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

559 được đề xuất ở đây. Chúng tôi đã sử dụng tên

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

249 để tránh phá vỡ mã hiện có xác định

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

559, cộng với chúng tôi cảm thấy tên đó truyền tải tốt hơn ngữ nghĩa của loại. Chúng tôi không bao gồm loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

562 vì nó được viết tầm thường là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

563.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

85

Cải thiện kiểm soát các bộ sửa đổi loại được ánh xạ

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

86

Lưu ý: Loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

249 là một triển khai thích hợp của loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

559 được đề xuất ở đây. Chúng tôi đã sử dụng tên

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

249 để tránh phá vỡ mã hiện có xác định

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

559, cộng với chúng tôi cảm thấy tên đó truyền tải tốt hơn ngữ nghĩa của loại. Chúng tôi không bao gồm loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

562 vì nó được viết tầm thường là

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

563.

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

87

Lưu ý: Loại ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];249 là một triển khai thích hợp của loại ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];559 được đề xuất ở đây. Chúng tôi đã sử dụng tên ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];249 để tránh phá vỡ mã hiện có xác định ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];559, cộng với chúng tôi cảm thấy tên đó truyền tải tốt hơn ngữ nghĩa của loại. Chúng tôi không bao gồm loại ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];562 vì nó được viết tầm thường là ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];563.

Cải thiện kiểm soát các bộ sửa đổi loại được ánh xạ

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

88

Lưu ý: Loại ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];249 là một triển khai thích hợp của loại ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];559 được đề xuất ở đây. Chúng tôi đã sử dụng tên ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];249 để tránh phá vỡ mã hiện có xác định ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];559, cộng với chúng tôi cảm thấy tên đó truyền tải tốt hơn ngữ nghĩa của loại. Chúng tôi không bao gồm loại ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];562 vì nó được viết tầm thường là ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];563.

Cải thiện kiểm soát các bộ sửa đổi loại được ánh xạ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

89

Hỗ trợ các loại được ánh xạ thêm một công cụ sửa đổi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118 hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

353 vào thuộc tính được ánh xạ, nhưng chúng không cung cấp hỗ trợ khả năng xóa các công cụ sửa đổi. Điều này quan trọng trong các loại được ánh xạ đồng nhất theo mặc định bảo tồn các biến đổi của loại cơ bản.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

90

TypeScript 2.8 Thêm khả năng cho một loại được ánh xạ để thêm hoặc xóa một công cụ sửa đổi cụ thể. Cụ thể, công cụ sửa đổi thuộc tính ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];118 hoặc ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];353 theo loại được ánh xạ hiện có thể được tiền tố với ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];266 hoặc ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];267 để chỉ ra rằng cần thêm hoặc loại bỏ công cụ sửa đổi.

Một công cụ sửa đổi không có tiền tố

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

266 hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

267 giống như một công cụ sửa đổi với tiền tố

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

266. Vì vậy, loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

573 trên tương ứng với

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

91

Sử dụng khả năng này, tsinterface SomeType { /** This is an index signature. */ [propName: string]: any;}function doStuff(value: SomeType) { let x = value["someProperty"];}43 hiện có loại ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];575 mới. Loại này dải ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];353 biến đổi từ tất cả các thuộc tính của ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];054, do đó làm cho tất cả các thuộc tính cần thiết.

Lưu ý rằng trong chế độ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

51, khi loại được ánh xạ đồng hình loại bỏ công cụ sửa đổi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

353 khỏi thuộc tính trong loại bên dưới, nó cũng loại bỏ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 khỏi loại thuộc tính đó:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

92

Cải thiện ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];407 với các loại giao lộ

Với TypeScript 2.8

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

407 được áp dụng cho một loại giao điểm được chuyển đổi thành một liên kết của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

407 được áp dụng cho mỗi thành phần giao lộ. Nói cách khác, các loại có dạng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

584 được chuyển đổi thành

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

585. Thay đổi này sẽ giải quyết sự không nhất quán với suy luận từ các biểu thức

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

407.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

93

Xử lý tốt hơn cho các mẫu không gian tên trong các tệp tsabstract class SuperClass { abstract someMethod(): void; badda() {}}type AbstractConstructor = abstract new (...args: any[]) => Tfunction withStyles>(Ctor: T) { abstract class StyledClass extends Ctor { getStyles() { // ... } } return StyledClass;}class SubClass extends withStyles(SuperClass) { someMethod() { this.someMethod() }}20

TypeScript 2.8 Thêm hỗ trợ để hiểu thêm các mẫu không gian tên trong các tệp

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

20. Các khai báo chữ cái trống ở cấp cao nhất, giống như các hàm và lớp, hiện được công nhận là khai báo không gian tên trong JavaScript.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

94

Bài tập ở cấp cao nhất nên hành xử theo cùng một cách; Nói cách khác, không cần phải tuyên bố ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];003 hoặc ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];178.

Iifes dưới dạng khai báo không gian tên

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

95

Generates:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

96

Lưu ý: Loại ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];249 là một triển khai thích hợp của loại ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];559 được đề xuất ở đây. Chúng tôi đã sử dụng tên ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];249 để tránh phá vỡ mã hiện có xác định ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];559, cộng với chúng tôi cảm thấy tên đó truyền tải tốt hơn ngữ nghĩa của loại. Chúng tôi không bao gồm loại ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];562 vì nó được viết tầm thường là ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];563.

Cải thiện kiểm soát các bộ sửa đổi loại được ánh xạ

Mới ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];602

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

602 cho phép chỉ tạo các tệp khai báo; ________ 720/________ 1605 Thế hệ đầu ra sẽ bị bỏ qua với cờ này. Cờ rất hữu ích khi thế hệ đầu ra

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

20 được xử lý bởi một bộ chuyển đổi khác như Babel.

TypeScript 2.7

Các thuộc tính được đặt tên liên tục

TypeScript 2.7 Thêm hỗ trợ để khai báo các thuộc tính được đặt tên trên các loại bao gồm các ký hiệu Ecmascript.

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

97

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

98

Điều này cũng áp dụng cho các chữ số và chuỗi.

Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

99

ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];607

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

97

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

98

Thí dụ

ts

// @errors: 2361

"foo" in 42;

00

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

97

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

98

Thí dụ

ts

// @errors: 2361

"foo" in 42;

01

tslet foo: [...string[], number];foo = [123];foo = ["hello", 123];foo = ["hello!", "hello!", "hello!", 123];let bar: [boolean, ...string[], boolean];bar = [true, false];bar = [true, "some text", false];bar = [true, "some", "separated", "text", false];97 tslet foo: [...string[], number];foo = [123];foo = ["hello", 123];foo = ["hello!", "hello!", "hello!", 123];let bar: [boolean, ...string[], boolean];bar = [true, false];bar = [true, "some text", false];bar = [true, "some", "separated", "text", false];98

Điều này cũng áp dụng cho các chữ số và chuỗi.

ts

// @errors: 2361

"foo" in 42;

02

Để cho phép các biểu tượng xử lý như là chữ viết độc đáo, loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

607 mới có sẵn.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

607 là phân nhóm của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

406 và chỉ được tạo ra từ việc gọi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

611 hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

612 hoặc từ các chú thích loại rõ ràng. Loại mới chỉ được phép trên các thuộc tính

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

178 và các thuộc tính

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

614 và để tham khảo một biểu tượng duy nhất cụ thể, bạn sẽ phải sử dụng toán tử

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

262. Mỗi tham chiếu đến một

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

607 ngụ ý một danh tính hoàn toàn duy nhất mà gắn liền với một tuyên bố nhất định.

Bởi vì mỗi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

607 có một danh tính hoàn toàn riêng biệt, không có hai loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

607 có thể gán hoặc tương đương với nhau.

ts

// @errors: 2361

"foo" in 42;

03

Khởi tạo lớp nghiêm ngặt

TypeScript 2.7 giới thiệu một lá cờ mới có tên tsinterface Options { /** File patterns to be excluded. */ exclude?: string[]; /** * It handles any extra properties that we haven't declared as type 'any'. */ [x: string]: any;}function processOptions(opts: Options) { // Notice we're *intentionally* accessing `excludes`, not `exclude` if (opts.excludes) { console.error( "The option `excludes` is not valid. Did you mean `exclude`?" ); }}75. Cờ này thực hiện kiểm tra để đảm bảo rằng mỗi thuộc tính thể hiện của một lớp được khởi tạo trong thân hàm tạo hoặc bởi một trình khởi tạo thuộc tính. Ví dụ

Ở trên, nếu chúng ta thực sự có ý định cho

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

51 có khả năng là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57, chúng ta nên tuyên bố nó với loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

622.

Có một số kịch bản nhất định trong đó các thuộc tính có thể được khởi tạo gián tiếp (có thể bằng phương pháp trợ giúp hoặc thư viện tiêm phụ thuộc), trong trường hợp đó bạn có thể sử dụng các sửa đổi xác nhận gán xác định mới cho các thuộc tính của bạn (thảo luận bên dưới).

ts

// @errors: 2361

"foo" in 42;

04

Hãy nhớ rằng

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

75 sẽ được bật cùng với các cờ chế độ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

30 khác, có thể ảnh hưởng đến dự án của bạn. Bạn có thể đặt cài đặt

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

75 thành

sh

tsc --explainFiles

23 trong ________ 598 của bạn

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

628 hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

629 trên dòng lệnh để tắt kiểm tra này.

ts

// @errors: 2361

"foo" in 42;

05

Xác nhận xác định xác định

ts

// @errors: 2361

"foo" in 42;

06

Khẳng định gán xác định là một tính năng cho phép đặt

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

35 sau thuộc tính ví dụ và khai báo biến để chuyển tiếp đến TypeScript rằng một biến thực sự được gán cho tất cả các ý định và mục đích, ngay cả khi các phân tích của TypeScript không thể phát hiện như vậy.

Ví dụ:

Với các xác nhận gán xác định, chúng tôi có thể khẳng định rằng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

631 thực sự được chỉ định bằng cách nối thêm một

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

35 vào tuyên bố của nó:

Theo một nghĩa nào đó, toán tử khẳng định gán xác định là kép của toán tử khẳng định không null (trong đó các biểu thức được cố định sau với

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

35), mà chúng ta cũng có thể đã sử dụng trong ví dụ.

Trong ví dụ của chúng tôi, chúng tôi biết rằng tất cả việc sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

631 sẽ được khởi tạo để sử dụng các xác nhận gán xác định hơn so với các xác nhận không null.

ts

// @errors: 2361

"foo" in 42;

07

Bộ đếm dài cố định

ts

// @errors: 2361

"foo" in 42;

08

Trong TypeScript 2.6 và trước đó,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

635 được coi là một loại phụ của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

636. Điều này được thúc đẩy bởi bản chất cấu trúc của TypeScript; Các phần tử thứ nhất và thứ hai của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

635 tương ứng là các kiểu con của các phần tử thứ nhất và thứ hai của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

636. Tuy nhiên, sau khi kiểm tra việc sử dụng các bộ dữ liệu trong thế giới thực, chúng tôi nhận thấy rằng hầu hết các tình huống trong đó điều này được cho phép thường không mong muốn.

Trong TypeScript 2.7, các bộ dữ liệu khác nhau không còn được gán cho nhau. Nhờ một yêu cầu kéo từ Kiara Grouwstra, các loại Tuple hiện mã hóa sự khác biệt của chúng thành loại thuộc tính ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];304 tương ứng của chúng. Điều này được thực hiện bằng cách tận dụng các loại chữ số, hiện cho phép các bộ dữ liệu khác biệt với các bộ dữ liệu của các loại khác nhau.

Về mặt khái niệm, bạn có thể coi loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

636 tương đương với tuyên bố sau đây của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

641:

Consider:

ts

// @errors: 2361

"foo" in 42;

09

Loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

110 trước đây đã được suy ra cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

645 và dòng thứ hai sau đó gây ra lỗi vì

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

645 dường như không có thuộc tính. Điều đó rõ ràng là lý tưởng.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

10

Nhiều suy luận loại theo nghĩa đen cho cùng một tham số loại được thu gọn tương tự thành một loại liên kết được chuẩn hóa duy nhất:

ts

// @errors: 2361

"foo" in 42;

11

Cải thiện xử lý các lớp giống hệt nhau về cấu trúc và các biểu thức ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];647

TypeScript 2.7 Cải thiện việc xử lý các lớp giống hệt nhau về mặt cấu trúc trong các loại liên minh và các biểu thức

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

647:

  • Các loại lớp giống hệt nhau về mặt cấu trúc, nhưng khác biệt hiện được bảo tồn trong các loại liên minh (thay vì loại bỏ tất cả trừ một).
  • Giảm phân nhóm loại Liên minh chỉ loại bỏ một loại lớp nếu nó là một lớp con và xuất phát từ loại lớp khác trong liên minh.
  • Kiểm tra loại của toán tử

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    647 hiện dựa trên việc liệu loại toán hạng bên trái có xuất phát từ loại được chỉ định bởi toán hạng bên phải hay không (trái ngược với kiểm tra tiểu loại cấu trúc).

Điều này có nghĩa là các loại liên minh và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

647 phân biệt đúng giữa các lớp giống hệt nhau về mặt cấu trúc.

Example:

ts

// @errors: 2361

"foo" in 42;

12

Loại bảo vệ được suy ra từ nhà điều hành tsinterface SomeType { /** This is an index signature. */ [propName: string]: any;}function doStuff(value: SomeType) { let x = value["someProperty"];}23

Toán tử

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

23 hiện hoạt động như một biểu thức thu hẹp cho các loại.

Cho một biểu thức

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

653, trong đó

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

142 là một loại theo nghĩa đen hoặc chuỗi theo nghĩa đen và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

631 là một loại liên minh, chi nhánh true true track thu hẹp thành một thuộc tính tùy chọn hoặc bắt buộc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

142 một thuộc tính tùy chọn hoặc thiếu

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

142.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

13

Hỗ trợ cho ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];658 Mẫu mô -đun phổ biến với ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];659

TypeScript 2.7 Cập nhật mô -đun CommonJS/AMD/UMD phát ra để tổng hợp các bản ghi không gian tên dựa trên sự hiện diện của chỉ báo

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

660 theo

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

661. Thay đổi mang lại đầu ra được tạo từ TypeScript gần hơn với đó được tạo bởi Babel.

Các mô -đun CommonJS/AMD/UMD trước đây được xử lý theo cách tương tự như các mô -đun ES6, dẫn đến một vài vấn đề. Cụ thể là:

  • TypeScript xử lý nhập không gian tên (tức là

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    662) cho mô -đun CommonJS/AMD/UMD tương đương với

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    663. Mọi thứ rất đơn giản ở đây, nhưng họ không làm việc nếu đối tượng chính được nhập là nguyên thủy hoặc lớp hoặc một chức năng. Thông số ECMAScript quy định rằng bản ghi không gian tên là một đối tượng đơn giản và việc nhập không gian tên (

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    70 trong ví dụ trên)

  • Tương tự, nhập mặc định (nghĩa là

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    665) cho mô -đun CommonJS/AMD/UMD tương đương với

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    666. Hầu hết các mô-đun CommonJS/AMD/UMD hiện nay không có xuất

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    667, làm cho mẫu nhập này thực tế không thể sử dụng các mô-đun không ES (tức là CommonJS/AMD/UMD). Ví dụ

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    668 hoặc

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    669 không được phép.

Theo

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

661 mới, hai vấn đề này sẽ được giải quyết:

  • Nhập không gian tên (nghĩa là

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    662) hiện được gắn cờ chính xác là không toàn cầu. Gọi nó sẽ dẫn đến một lỗi.
  • Nhập mặc định vào CommonJS/AMD/UMD hiện được cho phép (ví dụ:

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    668) và sẽ hoạt động như mong đợi.

LƯU Ý: Hành vi mới được thêm vào dưới một lá cờ để tránh bị vỡ không chính đáng vào các cơ sở mã hiện có. Chúng tôi khuyên bạn nên áp dụng nó cho cả các dự án mới và hiện tại. Đối với các dự án hiện tại, nhập khẩu không gian tên (

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

673) sẽ cần được chuyển đổi thành nhập khẩu mặc định (

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

674).

Thí dụ

Với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

661, hai người trợ giúp mới được tạo

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

676 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

677 để nhập

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

268 và nhập

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

667. Ví dụ, đầu vào như:

ts

// @errors: 2361

"foo" in 42;

14

Sẽ tạo ra:

ts

// @errors: 2361

"foo" in 42;

15

Phân tách số

TypeScript 2.7 mang lại hỗ trợ cho các dấu phân cách số ES. Các chữ số bây giờ có thể được tách thành các phân đoạn bằng cách sử dụng

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

52.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

16

Đầu ra sạch hơn ở chế độ ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];221

Chế độ TypeScript

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

221 hiện xóa màn hình sau khi yêu cầu biên dịch lại.

Đầu ra đẹp hơn ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];474

Cờ TypeScript

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

475 có thể làm cho thông báo lỗi dễ đọc và quản lý hơn.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

475 hiện sử dụng màu sắc cho tên tệp, mã chẩn đoán và số dòng. Tên và vị trí tệp hiện cũng được định dạng để cho phép điều hướng trong các thiết bị đầu cuối chung (ví dụ: thiết bị đầu cuối mã Visual Studio).

TypeScript 2.6

Các loại chức năng nghiêm ngặt

TypeScript 2.6 Giới thiệu cờ kiểm tra nghiêm ngặt mới,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

686. Công tắc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

686 là một phần của họ các công tắc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

30, có nghĩa là nó mặc định là ở chế độ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

30. Bạn có thể từ chối bằng cách đặt

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

690 trên dòng lệnh của mình hoặc trong tsconfig.json của bạn.

Trong các vị trí tham số loại chức năng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

686 được kiểm tra trái ngược thay vì bivariantly. Đối với một số nền tảng về phương sai có nghĩa là gì đối với các loại chức năng, hãy kiểm tra hiệp phương sai và trái ngược là gì ?.

Việc kiểm tra chặt chẽ hơn áp dụng cho tất cả các loại chức năng, ngoại trừ các loại có nguồn gốc trong các khai báo phương thức hoặc hàm tạo. Các phương pháp được loại trừ cụ thể để đảm bảo các lớp và giao diện chung (như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

692) tiếp tục liên quan chủ yếu liên quan.

Hãy xem xét ví dụ sau trong đó

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

693 là SuperType của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

694 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

695:

ts

// @errors: 2361

"foo" in 42;

17

Bài tập đầu tiên được phép trong chế độ kiểm tra loại mặc định, nhưng được gắn cờ là một lỗi trong chế độ loại chức năng nghiêm ngặt. Theo trực giác, chế độ mặc định cho phép gán bởi vì nó có thể là âm thanh, trong khi chế độ loại hàm nghiêm ngặt làm cho nó trở thành một lỗi vì nó không phải là âm thanh. Trong cả hai chế độ, gán thứ ba là một lỗi vì nó không bao giờ là âm thanh.

Một cách khác để mô tả ví dụ là loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

696 là bivariant (nghĩa là covariant hoặc contravariant) cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 trong chế độ kiểm tra loại mặc định, nhưng trái ngược với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 trong chế độ loại chức năng nghiêm ngặt.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

18

Bài tập đầu tiên bây giờ là một lỗi. Thực tế,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 là trái ngược trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

700 vì nó chỉ được sử dụng trong các vị trí tham số loại chức năng.

Nhân tiện, lưu ý rằng trong khi một số ngôn ngữ (ví dụ: C# và Scala) yêu cầu các chú thích phương sai (____ ____ 1701/________ 423 hoặc ________ 1266/________ 1267), phương sai xuất hiện tự nhiên từ việc sử dụng thực tế của tham số loại trong một loại chung do loại cấu trúc kiểu mẫu.

Note:

Theo

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

686, nhiệm vụ đầu tiên vẫn được phép nếu

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

706 được khai báo là một phương thức. Thực tế,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 là bivariant trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

700 vì nó chỉ được sử dụng trong các vị trí tham số phương thức.

ts

// @errors: 2361

"foo" in 42;

19

TypeScript 2.6 cũng cải thiện suy luận loại liên quan đến các vị trí của contravariant:

ts

// @errors: 2361

"foo" in 42;

20

Ở trên, tất cả các suy luận cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 bắt nguồn từ các vị trí trái ngược, và do đó chúng tôi suy ra phân nhóm phổ biến tốt nhất cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054. Điều này tương phản với các suy luận từ các vị trí đồng biến, nơi chúng tôi suy ra SuperType phổ biến tốt nhất.

Hỗ trợ cho cú pháp đoạn JSX

TypeScript 2.6.2 Thêm hỗ trợ cho cú pháp

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

711 mới cho các đoạn trong JSX. Nó thường được mong muốn để trả lại nhiều trẻ em từ một thành phần. Tuy nhiên, điều này là không hợp lệ, vì vậy phương pháp thông thường đã được bọc văn bản trong một yếu tố bổ sung, chẳng hạn như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

712 hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

713 như được hiển thị bên dưới.

ts

// @errors: 2361

"foo" in 42;

21

Để giải quyết mẫu này, React đã giới thiệu thành phần

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

02, cung cấp một cách chuyên dụng để bọc các phần tử như vậy mà không cần thêm một phần tử vào DOM. Tương ứng, cú pháp

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

711 đã được thêm vào JSX để tạo điều kiện cho cấu trúc mới này. Do đó, kịch bản trên trở thành:

ts

// @errors: 2361

"foo" in 42;

22

Theo

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

716, cú pháp mới không bị ảnh hưởng cho bản sao phát ra. Mặt khác, đối với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

717,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

711 được tổng hợp thành

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

719, trong đó

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

00 tôn trọng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

592. Lưu ý rằng đó là một lỗi khi sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

711 khi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

717 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

592 đều được bật.

Vui lòng tham khảo Blog React để biết thêm chi tiết về các đoạn và cú pháp mới.

Bộ đệm được gắn thẻ đối tượng mẫu trong các mô -đun

TypeScript 2.6 Khắc phục mẫu chuỗi được gắn thẻ phát ra để căn chỉnh tốt hơn với thông số kỹ thuật ECMAScript. Theo thông số kỹ thuật ECMAScript, mỗi khi thẻ mẫu được đánh giá, cùng một đối tượng chuỗi mẫu (cùng một

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

725) nên được truyền như đối số đầu tiên. Trước TypeScript 2.6, đầu ra được tạo là một đối tượng mẫu hoàn toàn mới mỗi lần. Mặc dù nội dung chuỗi giống nhau, nhưng điều này phát ra ảnh hưởng đến các thư viện sử dụng danh tính của chuỗi cho mục đích vô hiệu hóa bộ đệm, ví dụ: Lit-HTML.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

23

Bài tập đầu tiên bây giờ là một lỗi. Thực tế,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 là trái ngược trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

700 vì nó chỉ được sử dụng trong các vị trí tham số loại chức năng.

ts

// @errors: 2361

"foo" in 42;

24

Nhân tiện, lưu ý rằng trong khi một số ngôn ngữ (ví dụ: C# và Scala) yêu cầu các chú thích phương sai (____ ____ 1701/________ 423 hoặc ________ 1266/________ 1267), phương sai xuất hiện tự nhiên từ việc sử dụng thực tế của tham số loại trong một loại chung do loại cấu trúc kiểu mẫu.

Theo ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];686, nhiệm vụ đầu tiên vẫn được phép nếu ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];706 được khai báo là một phương thức. Thực tế, ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];054 là bivariant trong ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];700 vì nó chỉ được sử dụng trong các vị trí tham số phương thức.

TypeScript 2.6 cũng cải thiện suy luận loại liên quan đến các vị trí của contravariant:

Thí dụ

Bài tập đầu tiên bây giờ là một lỗi. Thực tế,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 là trái ngược trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

700 vì nó chỉ được sử dụng trong các vị trí tham số loại chức năng.

ts

// @errors: 2361

"foo" in 42;

25

Nhân tiện, lưu ý rằng trong khi một số ngôn ngữ (ví dụ: C# và Scala) yêu cầu các chú thích phương sai (____ ____ 1701/________ 423 hoặc ________ 1266/________ 1267), phương sai xuất hiện tự nhiên từ việc sử dụng thực tế của tham số loại trong một loại chung do loại cấu trúc kiểu mẫu.

ts

// @errors: 2361

"foo" in 42;

26

Theo

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

686, nhiệm vụ đầu tiên vẫn được phép nếu

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

706 được khai báo là một phương thức. Thực tế,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 là bivariant trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

700 vì nó chỉ được sử dụng trong các vị trí tham số phương thức.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

27

Bài tập đầu tiên bây giờ là một lỗi. Thực tế,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 là trái ngược trong

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

700 vì nó chỉ được sử dụng trong các vị trí tham số loại chức năng.

Nhân tiện, lưu ý rằng trong khi một số ngôn ngữ (ví dụ: C# và Scala) yêu cầu các chú thích phương sai (____ ____ 1701/________ 423 hoặc ________ 1266/________ 1267), phương sai xuất hiện tự nhiên từ việc sử dụng thực tế của tham số loại trong một loại chung do loại cấu trúc kiểu mẫu.

Nhanh hơn ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];733

TypeScript 2.6 mang đến việc triển khai

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

221 nhanh hơn. Phiên bản mới tối ưu hóa việc tạo mã và kiểm tra các cơ sở mã bằng các mô -đun ES. Các thay đổi được phát hiện trong một tệp mô -đun sẽ chỉ dẫn đến việc tái tạo mô -đun đã thay đổi và các tệp phụ thuộc vào nó, thay vì toàn bộ dự án. Các dự án có số lượng lớn các tệp sẽ gặt hái được nhiều lợi ích nhất từ ​​sự thay đổi này.

Việc triển khai mới cũng mang lại các cải tiến hiệu suất để xem trong TSServer. Logic Watcher đã được viết lại hoàn toàn để phản hồi nhanh hơn để thay đổi các sự kiện.

Các tài liệu tham khảo chỉ viết hiện được gắn cờ là không được sử dụng

TypeScript 2.6 bổ sung triển khai sửa đổi các tùy chọn trình biên dịch

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

54 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

736. Tuyên bố chỉ được viết cho nhưng không bao giờ đọc từ bây giờ được gắn cờ là không được sử dụng.

Thí dụ

Bellow cả

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

142 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

738 sẽ được đánh dấu là không sử dụng, bởi vì giá trị của chúng không bao giờ được đọc. Kiểu chữ trước sẽ chỉ kiểm tra xem các giá trị của chúng có được tham chiếu hay không.

ts

// @errors: 2361

"foo" in 42;

28

Ngoài ra các chức năng chỉ được gọi trong cơ thể của chính họ được coi là không sử dụng.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

29

Bellow cả ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];142 và ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];738 sẽ được đánh dấu là không sử dụng, bởi vì giá trị của chúng không bao giờ được đọc. Kiểu chữ trước sẽ chỉ kiểm tra xem các giá trị của chúng có được tham chiếu hay không.

Ngoài ra các chức năng chỉ được gọi trong cơ thể của chính họ được coi là không sử dụng.

TypeScript 2.5

ts

// @errors: 2361

"foo" in 42;

30

Tùy chọn tsinterface Options { /** File patterns to be excluded. */ exclude?: string[]; /** * It handles any extra properties that we haven't declared as type 'any'. */ [x: string]: any;}function processOptions(opts: Options) { // Notice we're *intentionally* accessing `excludes`, not `exclude` if (opts.excludes) { console.error( "The option `excludes` is not valid. Did you mean `exclude`?" ); }}84 Biến mệnh đề

Nhờ công việc được thực hiện bởi @tinganho, TypeScript 2.5 thực hiện một tính năng Ecmascript mới cho phép người dùng bỏ qua biến trong các điều khoản

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

84. Ví dụ: khi sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

741, bạn có thể cần phải kết thúc các cuộc gọi đến chức năng với ________ 1742/________ 584, nhưng bạn có thể không sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

744 bị ném khi đầu vào là sai.

ts

// @errors: 2361

"foo" in 42;

31

Loại xác nhận/cú pháp đúc trong chế độ ________ 544/________ 1746

TypeScript 2.5 giới thiệu khả năng khẳng định loại biểu thức khi sử dụng JavaScript đơn giản trong các dự án của bạn. Cú pháp là một nhận xét chú thích

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

747 theo sau là một biểu hiện dấu ngoặc đơn mà loại cần được đánh giá lại. Ví dụ:

Các gói được sao chép và chuyển hướng

Khi nhập bằng cách sử dụng Chiến lược độ phân giải mô -đun ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];748 trong TypeScript 2.5, trình biên dịch giờ đây sẽ kiểm tra xem các tệp có bắt nguồn từ các gói giống hệt nhau hay không. Nếu một tệp bắt nguồn từ một gói có ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];286 chứa cùng các trường ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];042 và ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];751 dưới dạng gói đã gặp trước đó, thì TypeScript sẽ chuyển hướng đến gói hàng đầu. Điều này giúp giải quyết các vấn đề trong đó hai gói có thể chứa các khai báo giống hệt nhau của các lớp, nhưng có chứa tsabstract class SuperClass { abstract someMethod(): void; badda() {}}type AbstractConstructor = abstract new (...args: any[]) => Tfunction withStyles>(Ctor: T) { abstract class StyledClass extends Ctor { getStyles() { // ... } } return StyledClass;}class SubClass extends withStyles(SuperClass) { someMethod() { this.someMethod() }}15 các thành viên khiến chúng không tương thích về mặt cấu trúc.

Như một phần thưởng tốt đẹp, điều này cũng có thể làm giảm bộ nhớ và dấu chân thời gian chạy của trình biên dịch và dịch vụ ngôn ngữ bằng cách tránh tải các tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 từ các gói trùng lặp.

Cờ trình biên dịch

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

754

TypeScript 2.5 mang cờ ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];755, tương đương với hành vi của cờ ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];756 trong Node.js. Cờ này cũng thể hiện hành vi ngược lại với tùy chọn WebPack ____ ____1757 (tức là cài đặt TypeScript từ ____ ____1755 đến ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];056 Parallels Cài đặt WebPack từ ____ ____1757 thành shtsc --explainFiles23 và ngược lại).

Trong chế độ này, các tham chiếu đến các mô -đun và gói (ví dụ: chỉ thị tsabstract class SuperClass { abstract someMethod(): void; badda() {}}type AbstractConstructor = abstract new (...args: any[]) => Tfunction withStyles>(Ctor: T) { abstract class StyledClass extends Ctor { getStyles() { // ... } } return StyledClass;}class SubClass extends withStyles(SuperClass) { someMethod() { this.someMethod() }}51S và ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];763) đều được giải quyết so với vị trí của tệp liên kết tượng trưng, ​​thay vì liên quan đến đường dẫn mà liên kết tượng trưng giải quyết. Để biết ví dụ cụ thể hơn, chúng tôi sẽ trì hoãn tài liệu trên trang web Node.js.

TypeScript 2.4

Biểu thức nhập khẩu động

ts

// @errors: 2361

"foo" in 42;

32

Các biểu thức

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

51 động là một tính năng mới và một phần của ECMAScript cho phép người dùng yêu cầu không đồng bộ một mô -đun tại bất kỳ điểm tùy ý nào trong chương trình của bạn.

Điều này có nghĩa là bạn có thể nhập một cách có điều kiện và lười biếng các mô -đun và thư viện khác. Ví dụ: ở đây, một hàm tsabstract class SuperClass { abstract someMethod(): void; badda() {}}type AbstractConstructor = abstract new (...args: any[]) => Tfunction withStyles>(Ctor: T) { abstract class StyledClass extends Ctor { getStyles() { // ... } } return StyledClass;}class SubClass extends withStyles(SuperClass) { someMethod() { this.someMethod() }}44 chỉ nhập thư viện tiện ích khi nó cần:

Nhiều gói có hỗ trợ cho các gói đầu ra tự động chia dựa trên các biểu thức

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

51 này, vì vậy hãy xem xét sử dụng tính năng mới này với mục tiêu mô -đun

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

59.

ts

// @errors: 2361

"foo" in 42;

33

Chuỗi enums

TypeScript 2.4 Bây giờ cho phép các thành viên ENUM chứa các bộ khởi tạo chuỗi.

Sự cảnh báo là các enum khởi tạo chuỗi có thể được ánh xạ ngược để có được tên thành viên enum ban đầu. Nói cách khác, bạn có thể viết

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

768 để lấy chuỗi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

769.

Cải thiện suy luận cho thuốc generic

TypeScript 2.4 giới thiệu một vài thay đổi tuyệt vời xung quanh cách suy luận thuốc generic.

ts

// @errors: 2361

"foo" in 42;

34

Trả về các loại như mục tiêu suy luận

ts

// @errors: 2361

"foo" in 42;

35

Đối với một, TypeScript giờ đây có thể đưa ra các suy luận cho loại trả về của một cuộc gọi. Điều này có thể cải thiện trải nghiệm của bạn và bắt lỗi. Một cái gì đó hiện đang hoạt động:

Trước TypeScript 2.4, trong ví dụ sau

ts

// @errors: 2361

"foo" in 42;

36

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

770 sẽ có loại

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

72. Điều này có nghĩa là chương trình sẽ kiểm tra loại, nhưng về mặt kỹ thuật bạn có thể làm bất cứ điều gì với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

770, chẳng hạn như sau:

ts

// @errors: 2361

"foo" in 42;

37

Ví dụ cuối cùng đó là thực sự an toàn.

Trong TypeScript 2.4, hàm ở phía bên phải ngầm đạt được các tham số loại và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

770 được suy ra để có loại tham số loại đó.

Nếu bạn sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

770 theo cách mà tham số loại ràng buộc khác không hỗ trợ, bạn sẽ nhận được lỗi chính xác. Trong trường hợp này, ràng buộc của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 là (ngầm)

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

110, vì vậy ví dụ cuối cùng sẽ thất bại một cách thích hợp.

Kiểm tra chặt chẽ hơn cho các chức năng chung

TypeScript bây giờ cố gắng thống nhất các tham số loại khi so sánh hai loại ký hiệu đơn. Do đó, bạn sẽ nhận được kiểm tra chặt chẽ hơn khi liên quan đến hai chữ ký chung và có thể bắt một số lỗi.

ts

// @errors: 2361

"foo" in 42;

38

Ngược lại đối với các tham số gọi lại

TypeScript luôn so sánh các tham số theo cách bivariant. Có một số lý do cho việc này, nhưng đây là vấn đề lớn đối với người dùng của chúng tôi cho đến khi chúng tôi thấy một số tác động bất lợi mà nó có với

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

16 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

778s.

TypeScript 2.4 giới thiệu việc thắt chặt điều này khi liên quan đến hai loại gọi lại. Ví dụ:

ts

// @errors: 2361

"foo" in 42;

39

Trước TypeScript 2.4, ví dụ này sẽ thành công. Khi liên quan đến các loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

216, TypeScript sẽ liên hệ hai bên tham số của chúng (nghĩa là loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

64). Khi liên quan đến mỗi

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

64, TypeScript cũng sẽ liên quan hai chiều về loại của các tham số đó.

Khi liên quan đến loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

216 trong TS 2.4, ngôn ngữ sẽ kiểm tra xem mỗi tham số có phải là loại gọi lại hay không, và nếu vậy, nó sẽ đảm bảo rằng các tham số đó được kiểm tra theo cách đối với mối quan hệ hiện tại.

Nói cách khác, TypeScript hiện bắt gặp lỗi trên, có thể là một sự thay đổi phá vỡ đối với một số người dùng, nhưng phần lớn sẽ hữu ích.

Phát hiện loại yếu

TypeScript 2.4 giới thiệu khái niệm về các loại yếu. Bất kỳ loại nào chứa không có gì ngoài một tập hợp các thuộc tính tùy chọn được coi là yếu. Ví dụ, loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

25 này là loại yếu:

ts

// @errors: 2361

"foo" in 42;

40

Trong TypeScript 2.4, giờ đây nó là một lỗi khi gán bất cứ điều gì cho một loại yếu khi không có sự chồng chéo trong các thuộc tính. Ví dụ:

ts

// @errors: 2361

"foo" in 42;

41

Bạn có thể nghĩ về điều này như là bản mô tả về việc tăng cường sự đảm bảo yếu của các loại này để bắt những gì sẽ là lỗi im lặng.

Vì đây là một sự thay đổi phá vỡ, bạn có thể cần biết về các cách giải quyết giống như những thay đổi đối với kiểm tra theo nghĩa đen của đối tượng nghiêm ngặt:

  1. Tuyên bố các tài sản nếu chúng thực sự tồn tại.
  2. Thêm một chữ ký chỉ mục vào loại yếu (nghĩa là

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    784).
  3. Sử dụng một khẳng định loại (nghĩa là

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    785).

TypeScript 2.3

Máy phát điện và Lặp lại cho ES5/ES3

Đầu tiên một số thuật ngữ ES2016:

Trình lặp

ES2015 đã giới thiệu

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

53, là một đối tượng phơi bày ba phương thức,

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

57,

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

39 và

sh

tsc --explainFiles

48, theo giao diện sau:

ts

// @errors: 2361

"foo" in 42;

42

Loại lặp này rất hữu ích để lặp lại các giá trị có sẵn đồng bộ, chẳng hạn như các phần tử của một mảng hoặc các khóa của bản đồ. Một đối tượng hỗ trợ lặp lại được cho là có thể sử dụng được nếu nó có phương thức

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

790 trả về một đối tượng

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

53.

Giao thức Iterator cũng xác định mục tiêu của một số tính năng ES2015 như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

792 và toán tử lây lan và phần còn lại mảng trong việc phá hủy các phép gán.

Máy phát điện

ES2015 cũng đã giới thiệu các máy phát điện trên mạng, là các chức năng có thể được sử dụng để mang lại kết quả tính toán một phần thông qua giao diện

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

53 và từ khóa

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

69. Máy phát điện cũng có thể ủy thác nội bộ các cuộc gọi cho một người khác thông qua

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

795. Ví dụ:

ts

// @errors: 2361

"foo" in 42;

43

Mới

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

796

Các trình tạo trước đây chỉ được hỗ trợ nếu mục tiêu là ES6/ES2015 trở lên. Hơn nữa, các cấu trúc hoạt động trên giao thức Iterator, ví dụ:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

792 chỉ được hỗ trợ nếu chúng hoạt động trên các mảng cho các mục tiêu dưới ES6/ES2015.

TypeScript 2.3 thêm hỗ trợ đầy đủ cho các trình tạo và giao thức Iterator cho các mục tiêu ES3 và ES5 với cờ

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

74.

Với

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

74, trình biên dịch sử dụng kiểm tra loại mới và phát hành hành vi cố gắng gọi phương thức

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

800 trên đối tượng lặp nếu nó được tìm thấy và tạo một trình lặp lại mảng tổng hợp trên đối tượng nếu không.

Xin lưu ý rằng điều này yêu cầu một

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

790 hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

790 Shim trong thời gian chạy cho bất kỳ giá trị không phải nào.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

792 Các câu lệnh, phá hủy mảng và các yếu tố lan truyền trong mảng, cuộc gọi và các biểu thức mới hỗ trợ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

790 trong ES5/E3 nếu có sẵn khi sử dụng

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

74, nhưng có thể được sử dụng trên một mảng ngay cả khi nó không xác định

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

790 tại thời gian chạy hoặc thời gian thiết kế.

Lặp lại không đồng bộ

TypeScript 2.3 bổ sung hỗ trợ cho các trình lặp và trình tạo async như được mô tả bởi đề xuất TC39 hiện tại.

Máy lặp không đồng bộ

Lặp đi lặp lại không đồng bộ giới thiệu

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

807, tương tự như

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

53. Sự khác biệt nằm ở thực tế là các phương thức

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

57,

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

39 và

sh

tsc --explainFiles

48 của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

807 trả về

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

16 cho kết quả lặp, thay vì kết quả. Điều này cho phép người gọi nhập ngũ vào một thông báo không đồng bộ trong thời gian

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

807 đã tiến đến điểm mang lại giá trị. Một

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

807 có hình dạng sau:

ts

// @errors: 2361

"foo" in 42;

44

Một đối tượng hỗ trợ phép lặp async được cho là có thể sử dụng được nếu nó có phương thức

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

816 trả về một đối tượng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

807.

Máy phát điện không đồng bộ

Đề xuất lặp Async giới thiệu các trình tạo async, đó là các hàm async cũng có thể được sử dụng để mang lại kết quả tính toán một phần. Máy phát điện ASYNC cũng có thể ủy thác các cuộc gọi qua

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

818 cho một điều đó có thể lặp lại hoặc không thể đồng bộ:

ts

// @errors: 2361

"foo" in 42;

45

Như với các trình tạo, các trình tạo async chỉ có thể là khai báo chức năng, biểu thức chức năng hoặc phương thức của các lớp hoặc nghĩa đen. Các hàm mũi tên không thể là máy phát Async. Các trình tạo async yêu cầu triển khai

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

16 toàn cầu hợp lệ (có thể là polyfill tương thích ES2015), ngoài tham chiếu

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

816 hợp lệ (có thể là ký hiệu gốc hoặc shim).

Tuyên bố

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

821

Cuối cùng, ES2015 đã giới thiệu tuyên bố

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

792 như một phương tiện để lặp lại một điều đáng tin cậy. Tương tự, đề xuất lặp Async giới thiệu câu lệnh

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

823 để lặp lại một sự khác biệt không thể đồng bộ:

ts

// @errors: 2361

"foo" in 42;

46

Tuyên bố

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

823 chỉ hợp pháp trong hàm ASYNC hoặc Trình tạo async.

Hãy cẩn thận

  • Hãy nhớ rằng sự hỗ trợ của chúng tôi cho các trình lặp Async dựa vào hỗ trợ cho

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    816 tồn tại trong thời gian chạy. Bạn có thể cần phải polyfill

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    816, cho mục đích đơn giản có thể đơn giản như:

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    827
  • Bạn cũng cần bao gồm

    ts

    abstract class SuperClass {

    abstract someMethod(): void;

    badda() {}

    }

    type AbstractConstructor<T> = abstract new (...args: any[]) => T

    function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

    abstract class StyledClass extends Ctor {

    getStyles() {

    // ...

    }

    }

    return StyledClass;

    }

    class SubClass extends withStyles(SuperClass) {

    someMethod() {

    this.someMethod()

    }

    }

    59 trong tùy chọn

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    272 của mình, để có được tuyên bố

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    807 nếu bạn chưa có nó.
  • Cuối cùng, nếu mục tiêu của bạn là ES5 hoặc ES3, bạn cũng sẽ cần đặt cờ

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    831.

Tham số chung mặc định

TypeScript 2.3 Thêm hỗ trợ để khai báo mặc định cho các tham số loại chung.

Thí dụ

Hãy xem xét một chức năng tạo ra một

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

832 mới, gọi nó không có đối số tạo ra

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

833; Bạn có thể tùy chọn vượt qua một danh sách trẻ em là tốt. Trước đây bạn sẽ phải định nghĩa nó là:

ts

// @errors: 2361

"foo" in 42;

47

Với mặc định tham số chung, chúng tôi có thể giảm nó thành:

ts

// @errors: 2361

"foo" in 42;

48

Mặc định tham số chung tuân theo các quy tắc sau:

  • Một tham số loại được coi là tùy chọn nếu nó có mặc định.
  • Các tham số loại bắt buộc không được tuân theo các tham số loại tùy chọn.
  • Các loại mặc định cho một tham số loại phải thỏa mãn ràng buộc cho tham số loại, nếu nó tồn tại.
  • Khi chỉ định các đối số loại, bạn chỉ được yêu cầu chỉ định các đối số loại cho các tham số loại bắt buộc. Các tham số loại không xác định sẽ giải quyết cho các loại mặc định của chúng.
  • Nếu một loại mặc định được chỉ định và suy luận không thể chọn một ứng cử viên, loại mặc định được suy ra.
  • Tuyên bố lớp hoặc giao diện hợp nhất với một lớp khai báo lớp hoặc giao diện hiện có có thể giới thiệu một mặc định cho một tham số loại hiện có.
  • Tuyên bố lớp hoặc giao diện hợp nhất với một lớp khai báo lớp hoặc giao diện hiện có có thể giới thiệu một tham số loại mới miễn là nó chỉ định mặc định.

Tùy chọn chính ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];834 mới

Kiểm tra mới được thêm vào TypeScript thường được tắt theo mặc định để tránh phá vỡ các dự án hiện có. Mặc dù tránh bị vỡ là một điều tốt, chiến lược này có nhược điểm là làm cho việc chọn mức độ an toàn loại cao nhất và làm như vậy yêu cầu hành động chọn tham gia rõ ràng trên mỗi bản phát hành TypeScript. Với tùy chọn

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

30, có thể chọn an toàn loại tối đa với sự hiểu biết rằng các lỗi bổ sung có thể được báo cáo bởi các phiên bản mới hơn của trình biên dịch khi các tính năng kiểm tra loại được cải thiện được thêm vào.

Tùy chọn trình biên dịch

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

30 mới thể hiện cài đặt được đề xuất của một số tùy chọn kiểm tra loại. Cụ thể, chỉ định

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

30 tương ứng với việc chỉ định tất cả các tùy chọn sau (và trong tương lai bao gồm nhiều tùy chọn hơn):

  • ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    51
  • ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    68
  • ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    840
  • ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    841

Trong các thuật ngữ chính xác, tùy chọn

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

30 đặt giá trị mặc định cho các tùy chọn trình biên dịch được liệt kê ở trên. Điều này có nghĩa là vẫn có thể kiểm soát riêng các tùy chọn. Ví dụ,

ts

// @errors: 2361

"foo" in 42;

49

Có tác dụng của việc bật tất cả các tùy chọn nghiêm ngặt ngoại trừ tùy chọn

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

840. Sử dụng sơ đồ này, có thể thể hiện các cấu hình bao gồm tất cả các tùy chọn nghiêm ngặt ngoại trừ một số tùy chọn được liệt kê rõ ràng. Nói cách khác, bây giờ có thể mặc định mức độ an toàn loại cao nhất nhưng từ chối một số séc nhất định.

Bắt đầu với TypeScript 2.3, mặc định

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98 được tạo bởi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

845 bao gồm cài đặt

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

846 trong phần

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

847. Do đó, các dự án mới bắt đầu với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

845 theo mặc định sẽ có mức an toàn loại cao nhất được bật.

Tăng cường đầu ra ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];849

Cùng với cài đặt

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

30 theo mặc định,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

845 có đầu ra nâng cao. Các tệp

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98 mặc định được tạo bởi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

845 hiện bao gồm một tập hợp các tùy chọn trình biên dịch phổ biến cùng với các mô tả của chúng được nhận xét. Chỉ cần bỏ theo cấu hình bạn muốn đặt để có được hành vi mong muốn; Chúng tôi hy vọng đầu ra mới sẽ đơn giản hóa việc thiết lập các dự án mới và giữ cho các tệp cấu hình có thể đọc được khi các dự án phát triển.

Lỗi trong các tệp .js với ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];854

Theo mặc định, trình biên dịch TypeScript không báo cáo bất kỳ lỗi nào trong các tệp .js bao gồm sử dụng

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

45. Với các lỗi kiểm tra loại 2.3 cũng có thể được báo cáo trong các tệp ____720 với

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

44.

Bạn có thể bỏ qua kiểm tra một số tệp bằng cách thêm

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

25 nhận xét cho họ; Ngược lại, bạn có thể chọn chỉ kiểm tra một vài tệp

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

20 bằng cách thêm

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

74 Nhận xét cho họ mà không cần thiết lập

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

44. Bạn cũng có thể bỏ qua các lỗi trên các dòng cụ thể bằng cách thêm

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

27 trên dòng trước.

Các tệp

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

20 vẫn được kiểm tra để đảm bảo rằng chúng chỉ bao gồm các tính năng Ecmascript tiêu chuẩn; Loại chú thích chỉ được phép trong các tệp

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

45 và được gắn cờ là lỗi trong các tệp

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

20. Nhận xét của JSDOC có thể được sử dụng để thêm một số thông tin loại vào mã JavaScript của bạn, xem tài liệu hỗ trợ JSDOC để biết thêm chi tiết về các cấu trúc JSDOC được hỗ trợ.

Xem Loại kiểm tra tài liệu tệp JavaScript để biết thêm chi tiết.

TypeScript 2.2

Hỗ trợ cho các lớp học hỗn hợp

TypeScript 2.2 bổ sung hỗ trợ cho mẫu lớp mixin ECMAScript 2015 (xem mô tả MDN Mixin và các loại hỗn hợp Real Real với các lớp JavaScript để biết thêm chi tiết) cũng như các quy tắc để kết hợp các chữ ký xây dựng mixin với chữ ký xây dựng thường xuyên trong các loại giao nhau.

Đầu tiên một số thuật ngữ:
  • Loại hàm tạo mixin đề cập đến một loại có một chữ ký cấu trúc duy nhất với một đối số còn lại là loại

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    538 và loại trả về giống như đối tượng. Ví dụ, được đưa ra một loại giống như đối tượng

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    364,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    868 là loại hàm tạo mixin với loại thể hiện

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    364.mixin constructor type refers to a type that has a single construct signature with a single rest argument of type

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    538 and an object-like return type. For example, given an object-like type

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    364,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    868 is a mixin constructor type with an instance type

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    364.

  • Một lớp mixin là một khai báo hoặc biểu thức lớp

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    281 biểu thức của loại tham số loại. Các quy tắc sau áp dụng cho các khai báo lớp mixin:mixin class is a class declaration or expression that

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    281 an expression of a type parameter type. The following rules apply to mixin class declarations:

  • Loại tham số loại của biểu thức

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    281 phải được hạn chế với loại chất xây dựng mixin.
  • Chất xây dựng của một lớp mixin (nếu có) phải có một tham số REST duy nhất là loại

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    538 và phải sử dụng toán tử lây lan để truyền các tham số đó làm đối số trong cuộc gọi

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    873.

Với một biểu thức

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

874 thuộc loại tham số

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 với ràng buộc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

364, lớp mixin

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

877 được xử lý như thể

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

874 có loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

364 và loại kết quả là giao điểm

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

80. Nói cách khác, một lớp mixin được biểu diễn dưới dạng giao điểm giữa loại hàm tạo lớp mixin và loại hàm tạo lớp cơ sở tham số.

Khi có được các chữ ký xây dựng của một loại giao điểm có chứa các loại hàm tạo mixin, chữ ký xây dựng mixin bị loại bỏ và các loại thể hiện của chúng được trộn vào các loại trả về của chữ ký xây dựng khác trong loại giao nhau. Ví dụ, loại giao điểm

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

881 có một chữ ký cấu trúc duy nhất

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

882.

Đặt tất cả các quy tắc trên lại với nhau trong một ví dụ:

ts

// @errors: 2361

"foo" in 42;

50

Các lớp mixin có thể hạn chế các loại lớp mà chúng có thể trộn vào bằng cách chỉ định loại trả về chữ ký xây dựng trong ràng buộc cho tham số loại. Ví dụ: chức năng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

883 sau đây thực hiện một nhà máy phân lớp con thêm phương thức

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

884 cho bất kỳ lớp nào thỏa mãn giao diện

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

043 (nghĩa là có các thuộc tính

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

631 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

770 của loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

06).

ts

// @errors: 2361

"foo" in 42;

51

ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];889 Loại

TypeScript không có một loại đại diện cho loại không định hướng, tức là bất kỳ điều gì không phải là

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

06 |

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79 |

sh

# Forward output to a text file

tsc --explainFiles > expanation.txt

# Pipe output to a utility program like `less`, or an editor like VS Code

tsc --explainFiles | less

tsc --explainFiles | code -

67 |

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

406 |

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 |

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57. Nhập loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

889 mới.

Với loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

889, API như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

898 có thể được thể hiện tốt hơn. Ví dụ:

ts

// @errors: 2361

"foo" in 42;

52

Hỗ trợ cho ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];899

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

899 META-PERTERTY là cú pháp mới được giới thiệu trong ES2015. Khi một thể hiện của một hàm tạo được tạo thông qua

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

901, giá trị của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

899 được đặt là tham chiếu đến hàm tạo hàm ban đầu được sử dụng để phân bổ thể hiện. Nếu một hàm được gọi là thay vì được xây dựng thông qua

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

901,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

899 sẽ được đặt thành

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57.

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

899 có ích khi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

907 hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

908 cần được đặt trong một hàm tạo lớp. Một trường hợp sử dụng như vậy là kế thừa từ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

276 trong NodeJS V4 trở lên.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

53

Điều này dẫn đến JS được tạo ra

ts

// @errors: 2361

"foo" in 42;

54

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

899 cũng có ích để viết các chức năng có thể xây dựng, ví dụ:

ts

// @errors: 2361

"foo" in 42;

55

Điều đó dịch thành:

ts

// @errors: 2361

"foo" in 42;

56

Kiểm tra tốt hơn cho ________ 607/________ 457 trong các toán học biểu thức

TypeScript 2.2 Cải thiện kiểm tra các toán hạng vô hiệu trong các biểu thức. Cụ thể, chúng hiện được gắn cờ là lỗi:

  • Nếu một trong hai toán tử của toán tử

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    266 là không thể điều chỉnh được và cả hai toán hạng không thuộc loại

    ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    72 hoặc

    ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    79.
  • Nếu một trong hai toán hạng của

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    267,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    268,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    918,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    919,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    920,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    921,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    922,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    923,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    924,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    925
  • Nếu một trong hai toán hạng của A

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    927,

    ts

    abstract class Shape {

    abstract getArea(): number;

    }

    // ---cut---

    interface HasArea {

    getArea(): number;

    }

    // Works!

    let Ctor: abstract new () => HasArea = Shape;

    61,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    929,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    930 hoặc

    ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    23 toán tử là không thể.
  • Nếu toán hạng bên phải của toán tử

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    647 là không thể.
  • Nếu toán hạng của

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    266,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    267,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    935,

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    936 hoặc

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    937 Toán tử Unary là không thể.

Một toán hạng được coi là vô hiệu hóa nếu loại của toán hạng là

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 hoặc một loại liên minh bao gồm

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57. Lưu ý rằng trường hợp loại Liên minh chỉ chỉ xảy ra ở chế độ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

51 vì

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 biến mất khỏi các công đoàn ở chế độ kiểm tra loại cổ điển.

Thuộc tính chấm cho các loại có chữ ký chỉ mục chuỗi

Các loại có chữ ký chỉ mục chuỗi có thể được lập chỉ mục bằng ký hiệu

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

945, nhưng không được phép sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

946. Bắt đầu với TypeScript 2.2 bằng cách sử dụng một trong hai nên được cho phép.

ts

// @errors: 2361

"foo" in 42;

57

Điều này chỉ áp dụng cho các loại có chữ ký chỉ mục chuỗi rõ ràng. Vẫn là một lỗi khi truy cập các thuộc tính không xác định trên một loại bằng ký hiệu

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

946.

Hỗ trợ cho người vận hành lây lan trên các yếu tố JSX Trẻ em

TypeScript 2.2 bổ sung hỗ trợ cho việc sử dụng chênh lệch cho trẻ em phần tử JSX. Vui lòng xem Facebook/JSX#57 để biết thêm chi tiết.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

58

Mới ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];948

Đường ống xây dựng bản địa phản ứng mong đợi tất cả các tệp sẽ có phần mở rộng

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

20 ngay cả khi tệp chứa cú pháp JSX. Giá trị

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

50 mới

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

951 sẽ kiên trì cú pháp JSX trong tệp đầu ra, nhưng cung cấp cho nó một phần mở rộng

ts

abstract class SuperClass {

abstract someMethod(): void;

badda() {}

}

type AbstractConstructor<T> = abstract new (...args: any[]) => T

function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

abstract class StyledClass extends Ctor {

getStyles() {

// ...

}

}

return StyledClass;

}

class SubClass extends withStyles(SuperClass) {

someMethod() {

this.someMethod()

}

}

20.

TypeScript 2.1

ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];407 và các loại tra cứu

Trong JavaScript, điều khá phổ biến là có API mong đợi tên thuộc tính là tham số, nhưng cho đến nay, nó có thể thể hiện các mối quan hệ loại xảy ra trong các API đó.

Nhập truy vấn loại chỉ mục hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

407; Một truy vấn loại được lập chỉ mục

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

412 mang lại loại tên thuộc tính được phép cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054. Một loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

412 được coi là một kiểu con của

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

79.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

59

Dual của điều này là các loại truy cập được lập chỉ mục, còn được gọi là các loại tra cứu. Về mặt cú pháp, chúng trông giống hệt như một truy cập phần tử, nhưng được viết dưới dạng các loại:

Thí dụ

ts

// @errors: 2361

"foo" in 42;

60

Bạn có thể sử dụng mẫu này với các phần khác của hệ thống loại để tra cứu an toàn kiểu.

ts

// @errors: 2361

"foo" in 42;

61

Các loại được ánh xạ

Một nhiệm vụ phổ biến là lấy một loại hiện có và làm cho mỗi thuộc tính của nó hoàn toàn tùy chọn. Hãy nói rằng chúng tôi có một `người:

ts

// @errors: 2361

"foo" in 42;

62

Một phiên bản một phần của nó sẽ là:

ts

// @errors: 2361

"foo" in 42;

63

Với các loại được ánh xạ,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

959 có thể được viết dưới dạng chuyển đổi tổng quát trên loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

037 như:

ts

// @errors: 2361

"foo" in 42;

64

Các loại được ánh xạ được tạo ra bằng cách lấy một liên kết các loại nghĩa đen và tính toán một tập hợp các thuộc tính cho một loại đối tượng mới. Họ giống như các toàn bộ danh sách trong Python, nhưng thay vì tạo ra các yếu tố mới trong danh sách, chúng tạo ra các thuộc tính mới trong một loại.

Ngoài

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

961, các loại được ánh xạ có thể diễn đạt nhiều biến đổi hữu ích trên các loại:

ts

// @errors: 2361

"foo" in 42;

65

ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];961, ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];171, ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];964 và ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];248

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

961 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

171, như được mô tả trước đó, là những cấu trúc rất hữu ích. Bạn có thể sử dụng chúng để mô tả một số thói quen JS phổ biến như:

ts

// @errors: 2361

"foo" in 42;

66

Do đó, chúng hiện được bao gồm theo mặc định trong thư viện tiêu chuẩn.

Chúng tôi cũng bao gồm hai loại tiện ích khác:

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

964 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

248.

ts

// @errors: 2361

"foo" in 42;

67

ts

// @errors: 2361

"foo" in 42;

68

Đối tượng lan rộng và nghỉ ngơi

TypeScript 2.1 mang lại sự hỗ trợ cho sự lan truyền và nghỉ ngơi của ES2017.

Tương tự như lan truyền mảng, việc truyền bá một đối tượng có thể tiện dụng để có được một bản sao nông:

ts

// @errors: 2361

"foo" in 42;

69

Tương tự, bạn có thể hợp nhất một số đối tượng khác nhau. Trong ví dụ sau,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

970 sẽ có các thuộc tính từ

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

70,

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

55 và

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

51.

ts

// @errors: 2361

"foo" in 42;

70

Bạn cũng có thể ghi đè các thuộc tính hiện có và thêm các thuộc tính mới:

ts

// @errors: 2361

"foo" in 42;

71

Thứ tự chỉ định các hoạt động lan truyền xác định các thuộc tính nào kết thúc trong đối tượng kết quả; Các thuộc tính trong sau này chênh lệch chiến thắng trên các thuộc tính được tạo trước đó.

Phần còn lại của đối tượng là kép của sự lây lan đối tượng, trong đó chúng có thể trích xuất bất kỳ thuộc tính bổ sung nào mà don don được chọn khi phá hủy một yếu tố:

ts

// @errors: 2361

"foo" in 42;

72

Hàm Async DownLevel

Tính năng này được hỗ trợ trước TypeScript 2.1, nhưng chỉ khi nhắm mục tiêu ES6/ES2015. TypeScript 2.1 mang đến khả năng cho ES3 và ES5 Run-Times, có nghĩa là bạn sẽ được tự do tận dụng nó bất kể bạn sử dụng môi trường nào.

Lưu ý: Đầu tiên, chúng tôi cần đảm bảo thời gian chạy của chúng tôi có khả năng tuân thủ ECMAScript

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

16 trên toàn cầu. Điều đó có thể liên quan đến việc lấy một polyfill cho

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

16 hoặc dựa vào một polyfill mà bạn có thể có trong thời gian chạy mà bạn đang nhắm mục tiêu. Chúng tôi cũng cần đảm bảo rằng TypeScript biết

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

16 tồn tại bằng cách đặt tùy chọn

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

272 của bạn thành một cái gì đó như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

978 hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

979

Thí dụ
tsconfig.json

ts

// @errors: 2361

"foo" in 42;

73
dramaticWelcome.ts

ts

// @errors: 2361

"foo" in 42;

74

Biên dịch và chạy đầu ra sẽ dẫn đến hành vi chính xác trên động cơ ES3/ES5.

Hỗ trợ cho Thư viện Trợ giúp Bên ngoài (ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];728)

TypeScript đưa ra một số ít các chức năng trợ giúp như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

981 để thừa kế,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

982 cho toán tử lây lan trong các yếu tố chữ viết và các phần tử JSX và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

983 cho các hàm ASYNC.

Trước đây có hai tùy chọn:

  1. Tiêm người trợ giúp trong mọi tệp cần chúng hoặc
  2. Không có người trợ giúp nào với

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    984.

Hai tùy chọn còn lại nhiều hơn mong muốn; Gói người trợ giúp trong mỗi tập tin là một điểm đau cho khách hàng cố gắng giữ cho kích thước gói của họ nhỏ. Và không bao gồm người trợ giúp, có nghĩa là khách hàng phải duy trì thư viện người trợ giúp của riêng họ.

TypeScript 2.1 cho phép đưa các tệp này vào dự án của bạn một lần vào một mô -đun riêng và trình biên dịch sẽ phát ra nhập vào chúng khi cần thiết.

Đầu tiên, cài đặt thư viện tiện ích

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

728:

ts

// @errors: 2361

"foo" in 42;

75

Thứ hai, biên dịch các tệp của bạn bằng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

727:

ts

// @errors: 2361

"foo" in 42;

76

Vì vậy, với đầu vào sau, tệp ____720 kết quả sẽ bao gồm nhập vào

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

728 và sử dụng trình trợ giúp

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

982 từ nó thay vì nội tuyến.

ts

// @errors: 2361

"foo" in 42;

77

ts

// @errors: 2361

"foo" in 42;

78

Nhập khẩu không cần thiết

Theo truyền thống, TypeScript đã quá nghiêm ngặt về cách bạn có thể nhập các mô -đun. Điều này là để tránh lỗi chính tả và ngăn người dùng sử dụng các mô -đun không chính xác.

Tuy nhiên, rất nhiều thời gian, bạn có thể chỉ muốn nhập một mô -đun hiện có có thể không có tệp

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

19 riêng. Trước đây đây là một lỗi. Bắt đầu với TypeScript 2.1 Điều này bây giờ dễ dàng hơn nhiều.

Với TypeScript 2.1, bạn có thể nhập mô -đun JavaScript mà không cần khai báo loại. Một khai báo loại (chẳng hạn như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

991 hoặc

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

992) vẫn được ưu tiên nếu nó tồn tại.

Nhập vào một mô -đun không có tệp khai báo vẫn sẽ được gắn cờ là lỗi theo

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

68.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

79

Biên dịch và chạy đầu ra sẽ dẫn đến hành vi chính xác trên động cơ ES3/ES5.

Hỗ trợ cho Thư viện Trợ giúp Bên ngoài (

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

728)

TypeScript đưa ra một số ít các chức năng trợ giúp như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

981 để thừa kế,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

982 cho toán tử lây lan trong các yếu tố chữ viết và các phần tử JSX và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

983 cho các hàm ASYNC.

Trước đây có hai tùy chọn:

Tiêm người trợ giúp trong mọi tệp cần chúng hoặc

Không có người trợ giúp nào với ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];984.

Hai tùy chọn còn lại nhiều hơn mong muốn; Gói người trợ giúp trong mỗi tập tin là một điểm đau cho khách hàng cố gắng giữ cho kích thước gói của họ nhỏ. Và không bao gồm người trợ giúp, có nghĩa là khách hàng phải duy trì thư viện người trợ giúp của riêng họ.

ts

// @errors: 2361

"foo" in 42;

80

TypeScript 2.1 cho phép đưa các tệp này vào dự án của bạn một lần vào một mô -đun riêng và trình biên dịch sẽ phát ra nhập vào chúng khi cần thiết.

Đầu tiên, cài đặt thư viện tiện ích

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

728:

Thí dụ

ts

// @errors: 2361

"foo" in 42;

81

Biên dịch và chạy đầu ra sẽ dẫn đến hành vi chính xác trên động cơ ES3/ES5.

Hỗ trợ cho Thư viện Trợ giúp Bên ngoài (

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

728)

ts

// @errors: 2361

"foo" in 42;

82

TypeScript đưa ra một số ít các chức năng trợ giúp như ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];981 để thừa kế, ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];982 cho toán tử lây lan trong các yếu tố chữ viết và các phần tử JSX và ts// A tuple that stores a pair of numberslet a: [number, number] = [1, 2];// A tuple that stores a string, a number, and a booleanlet b: [string, number, boolean] = ["hello", 42, true];983 cho các hàm ASYNC.

Trước đây có hai tùy chọn:

Thí dụ

ts

// @errors: 2361

"foo" in 42;

83

Biên dịch và chạy đầu ra sẽ dẫn đến hành vi chính xác trên động cơ ES3/ES5.

Hỗ trợ cho Thư viện Trợ giúp Bên ngoài (

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

728)

TypeScript đưa ra một số ít các chức năng trợ giúp như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

981 để thừa kế,

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

982 cho toán tử lây lan trong các yếu tố chữ viết và các phần tử JSX và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

983 cho các hàm ASYNC.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

84

Mở rộng loại theo nghĩa đen có thể được kiểm soát thông qua các chú thích loại rõ ràng. Cụ thể, khi một biểu thức của một loại theo nghĩa đen được suy ra cho một vị trí const mà không có chú thích loại, biến

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

178 sẽ được đưa ra một loại nghĩa đen mở rộng. Nhưng khi một vị trí

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

178 có chú thích loại theo nghĩa đen rõ ràng, biến

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

178 có loại nghĩa đen không mở rộng.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

85

Sử dụng các giá trị được trả về từ các cuộc gọi siêu

Trong ES2015, các nhà xây dựng trả về một đối tượng hoàn toàn thay thế giá trị của

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 cho bất kỳ người gọi nào của

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

037. Do đó, cần phải nắm bắt bất kỳ giá trị trả về tiềm năng nào của

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

037 và thay thế nó bằng

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036. Thay đổi này cho phép làm việc với các yếu tố tùy chỉnh, tận dụng điều này để khởi tạo các yếu tố được phân bổ trình duyệt với các trình xây dựng viết người dùng.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

86

Generates:

ts

// @errors: 2361

"foo" in 42;

87

Thay đổi này đòi hỏi phải có một hành vi mở rộng các lớp tích hợp như

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

276,

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

19,

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

042, v.v.

Kế thừa cấu hình

Thông thường một dự án có nhiều mục tiêu đầu ra, ví dụ:

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

043 và

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

044, gỡ lỗi và sản xuất hoặc

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

045 và

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

046; Chỉ cần một vài tùy chọn cấu hình thay đổi giữa hai mục tiêu này và việc duy trì nhiều tệp

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98 có thể là một rắc rối.

TypeScript 2.1 hỗ trợ cấu hình kế thừa bằng cách sử dụng

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

281, trong đó:

  • ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    281 là một tài sản cấp cao nhất trong

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    98 (cùng với

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    628,

    ts

    let foo: [...string[], number];

    foo = [123];

    foo = ["hello", 123];

    foo = ["hello!", "hello!", "hello!", 123];

    let bar: [boolean, ...string[], boolean];

    bar = [true, false];

    bar = [true, "some text", false];

    bar = [true, "some", "separated", "text", false];

    052,

    ts

    let foo: [...string[], number];

    foo = [123];

    foo = ["hello", 123];

    foo = ["hello!", "hello!", "hello!", 123];

    let bar: [boolean, ...string[], boolean];

    bar = [true, false];

    bar = [true, "some text", false];

    bar = [true, "some", "separated", "text", false];

    053 và

    ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    45).
  • Giá trị của

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    281 phải là một chuỗi chứa một đường dẫn đến tệp cấu hình khác để kế thừa.
  • Cấu hình từ tệp cơ sở được tải trước, sau đó được ghi đè bởi các tệp trong tệp cấu hình kế thừa.
  • Không cho phép thông tư giữa các tệp cấu hình.
  • ts

    let foo: [...string[], number];

    foo = [123];

    foo = ["hello", 123];

    foo = ["hello!", "hello!", "hello!", 123];

    let bar: [boolean, ...string[], boolean];

    bar = [true, false];

    bar = [true, "some text", false];

    bar = [true, "some", "separated", "text", false];

    052,

    ts

    let foo: [...string[], number];

    foo = [123];

    foo = ["hello", 123];

    foo = ["hello!", "hello!", "hello!", 123];

    let bar: [boolean, ...string[], boolean];

    bar = [true, false];

    bar = [true, "some text", false];

    bar = [true, "some", "separated", "text", false];

    053 và

    ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    45 từ tệp cấu hình kế thừa ghi đè lên các tệp từ tệp cấu hình cơ sở.
  • Tất cả các đường dẫn tương đối được tìm thấy trong tệp cấu hình sẽ được giải quyết liên quan đến tệp cấu hình mà chúng có nguồn gốc.
Thí dụ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

059:

ts

// @errors: 2361

"foo" in 42;

88

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98:

ts

// @errors: 2361

"foo" in 42;

89

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

061:

ts

// @errors: 2361

"foo" in 42;

90

Mới tslet foo: [...string[], number];foo = [123];foo = ["hello", 123];foo = ["hello!", "hello!", "hello!", 123];let bar: [boolean, ...string[], boolean];bar = [true, false];bar = [true, "some text", false];bar = [true, "some", "separated", "text", false];062

Gọi trình biên dịch với

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

841 Nguyên nhân:

  1. Phân tích tất cả các mã trong chế độ nghiêm ngặt.
  2. Viết chỉ thị

    ts

    let foo: [...string[], number];

    foo = [123];

    foo = ["hello", 123];

    foo = ["hello!", "hello!", "hello!", 123];

    let bar: [boolean, ...string[], boolean];

    bar = [true, false];

    bar = [true, "some text", false];

    bar = [true, "some", "separated", "text", false];

    064 trên đỉnh mỗi tệp được tạo.

Các mô -đun được phân tích cú pháp tự động ở chế độ nghiêm ngặt. Cờ mới được khuyến nghị cho mã không mô-đun.

TypeScript 2.0

Các loại không xác định và không xác định

TypeScript có hai loại đặc biệt, NULL và không xác định, có các giá trị tương ứng

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57. Trước đây không thể đặt tên rõ ràng các loại này, nhưng

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 hiện có thể được sử dụng làm tên loại bất kể chế độ kiểm tra loại.

Trình kiểm tra loại trước đây đã xem xét

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 có thể gán cho bất cứ điều gì. Thực tế,

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 là các giá trị hợp lệ của mọi loại và không thể loại trừ cụ thể chúng (và do đó không thể phát hiện sử dụng sai số của chúng).

tslet foo: [...string[], number];foo = [123];foo = ["hello", 123];foo = ["hello!", "hello!", "hello!", 123];let bar: [boolean, ...string[], boolean];bar = [true, false];bar = [true, "some text", false];bar = [true, "some", "separated", "text", false];073

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

51 chuyển sang chế độ kiểm tra null nghiêm ngặt mới.

Trong chế độ kiểm tra null nghiêm ngặt, các giá trị

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 không nằm trong miền của mọi loại và chỉ có thể gán cho chính họ và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

72 (một ngoại lệ là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 cũng có thể được gán cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

348). Vì vậy, trong khi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 và

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

081 được coi là đồng nghĩa trong chế độ kiểm tra loại thông thường (vì

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 được coi là một kiểu con của bất kỳ

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054), chúng là các loại khác nhau trong chế độ kiểm tra loại nghiêm ngặt và chỉ

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

081 cho phép

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 giá trị. Điều tương tự cũng đúng với mối quan hệ của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

054 với

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

087.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

91

Kiểm tra trước khi sử dụng

Trong chế độ kiểm tra null nghiêm ngặt, trình biên dịch yêu cầu mọi tham chiếu đến một biến cục bộ của một loại không bao gồm

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 để được gán trước biến đó trong mọi đường dẫn mã trước có thể.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

92

Trình biên dịch kiểm tra các biến chắc chắn được gán bằng cách thực hiện phân tích loại dựa trên luồng điều khiển. Xem sau để biết thêm chi tiết về chủ đề này.

Các tham số và thuộc tính tùy chọn

Các tham số và thuộc tính tùy chọn tự động có

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 được thêm vào các loại của chúng, ngay cả khi các chú thích loại của chúng không đặc biệt bao gồm

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57. Ví dụ, hai loại sau đây là giống hệt nhau:

ts

// @errors: 2361

"foo" in 42;

93

Những người bảo vệ loại không null và không xác định

Truy cập thuộc tính hoặc lệnh gọi hàm tạo ra lỗi thời gian biên dịch nếu đối tượng hoặc hàm thuộc loại bao gồm

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57. Tuy nhiên, các bảo vệ loại được mở rộng để hỗ trợ kiểm tra không phải và không xác định.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

94

Các bảo vệ loại không null và không xác định có thể sử dụng nhà điều hành

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

093,

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

094,

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

095 hoặc

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

096 để so sánh với

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57, như trong

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

099 hoặc

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

100. Các hiệu ứng đối với các loại biến chủ thể phản ánh chính xác ngữ nghĩa JavaScript (ví dụ: toán tử công bằng kép kiểm tra cả hai giá trị cho dù có gì được chỉ định trong khi chỉ số ba chỉ kiểm tra giá trị được chỉ định).

Tên chấm trong bộ bảo vệ loại

Loại bảo vệ trước đây chỉ hỗ trợ kiểm tra các biến và tham số cục bộ. Loại bảo vệ hiện đang hỗ trợ kiểm tra các tên chấm chấm trên mạng bao gồm một tên biến hoặc tên tham số theo một hoặc nhiều truy cập thuộc tính.

Thí dụ

ts

// @errors: 2361

"foo" in 42;

95

Loại bảo vệ cho tên chấm cũng hoạt động với các chức năng bảo vệ loại do người dùng xác định và các toán tử

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

262 và

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

647 và không phụ thuộc vào tùy chọn trình biên dịch

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

51.

Người bảo vệ loại cho một tên chấm chấm không có hiệu lực sau khi gán cho bất kỳ phần nào của tên chấm. Ví dụ: một người bảo vệ loại cho

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

104 sẽ không có hiệu lực sau khi được gán cho

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

631,

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

106 hoặc

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

104.

Toán tử biểu hiện

Các toán tử biểu hiện cho phép các loại toán học bao gồm

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 và/hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 nhưng luôn tạo ra các giá trị của các loại không phải null và không xác định.

ts

// @errors: 2361

"foo" in 42;

96

Toán tử

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

49 thêm

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 và/hoặc

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 vào loại toán hạng bên phải tùy thuộc vào loại hoạt động bên trái và toán tử

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

50 loại bỏ cả

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 .

ts

// @errors: 2361

"foo" in 42;

97

Loại mở rộng

Các loại

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 không được mở rộng thành

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

72 trong chế độ kiểm tra null nghiêm ngặt.

ts

// @errors: 2361

"foo" in 42;

98

Trong chế độ kiểm tra loại thông thường, loại được suy ra của

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

90 là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

72 do mở rộng, nhưng trong chế độ kiểm tra null nghiêm ngặt, loại được suy ra của

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

90 là

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 (và do đó, không có một loại chú thích,

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 là giá trị duy nhất có thể có đối với

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

90).

Toán tử khẳng định không null

Một toán tử biểu thức sau sửa lỗi mới có thể được sử dụng để khẳng định rằng toán hạng của nó không phải là null và không được xác định trong các bối cảnh mà trình kiểm tra loại không thể kết luận thực tế đó. Cụ thể, hoạt động

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

126 tạo ra giá trị của loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

631 với

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 bị loại trừ. Tương tự như các xác nhận loại của các biểu mẫu

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

130 và

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

131, toán tử xác nhận không null

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

35 được xóa đơn giản trong mã JavaScript phát ra.

ts

// @errors: 2361

"foo" in 42;

99

Khả năng tương thích

Các tính năng mới được thiết kế sao cho chúng có thể được sử dụng ở cả chế độ kiểm tra null nghiêm ngặt và chế độ kiểm tra loại thông thường. Cụ thể, các loại

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

07 và

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57 được tự động xóa khỏi các loại liên kết ở chế độ kiểm tra loại thông thường (vì chúng là các kiểu con của tất cả các loại khác) và toán tử biểu thức xác nhận không null

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

35 được cho phép nhưng không có tác dụng trong chế độ kiểm tra loại thông thường . Do đó, các tệp khai báo được cập nhật để sử dụng các loại nhận biết không xác định và không xác định vẫn có thể được sử dụng trong chế độ kiểm tra loại thông thường để tương thích ngược.

Trong các thuật ngữ thực tế, chế độ kiểm tra null nghiêm ngặt yêu cầu tất cả các tệp trong phần biên dịch đều không nhận thức được và không xác định.

Phân tích loại dựa trên luồng kiểm soát

TypeScript 2.0 thực hiện phân tích loại dựa trên luồng điều khiển cho các biến và tham số cục bộ. Trước đây, phân tích loại được thực hiện cho các bảo vệ loại được giới hạn trong các câu lệnh

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

82 và các biểu thức có điều kiện

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

137 và didn bao gồm các hiệu ứng của các bài tập và các cấu trúc luồng kiểm soát như các câu lệnh

ts

abstract class Shape {

abstract getArea(): number;

}

// ---cut---

interface HasArea {

getArea(): number;

}

// Works!

let Ctor: abstract new () => HasArea = Shape;

39 và

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

139. Với TypeScript 2.0, Trình kiểm tra loại phân tích tất cả các luồng điều khiển có thể có trong các câu lệnh và biểu thức để tạo loại cụ thể nhất có thể (loại thu hẹp) tại bất kỳ vị trí nào cho biến hoặc tham số cục bộ được tuyên bố là có loại liên kết.

Thí dụ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

00

Phân tích loại dựa trên luồng điều khiển có liên quan đến các loại liên quan đến các loại

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

51 vì các loại vô hiệu được biểu diễn bằng cách sử dụng các loại liên kết:

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

01

Hơn nữa, trong chế độ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

51, phân tích loại dựa trên luồng điều khiển bao gồm phân tích gán xác định cho các biến cục bộ của các loại không cho phép giá trị

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

57.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

02

Các loại liên minh được gắn thẻ

TypeScript 2.0 thực hiện hỗ trợ cho các loại liên minh được gắn thẻ (hoặc phân biệt đối xử). Cụ thể, trình biên dịch TS hiện hỗ trợ các loại bảo vệ loại mà các loại liên minh hẹp dựa trên các thử nghiệm về tài sản phân biệt đối xử và hơn nữa mở rộng khả năng đó cho các câu lệnh

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

143.

Thí dụ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

03

Người bảo vệ loại thuộc tính phân biệt đối xử là một biểu thức của dạng

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

144,

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

145,

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

146 hoặc

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

147, trong đó

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

148 và

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

149 là một thuộc tính và biểu thức của một loại theo nghĩa đen hoặc một loại của chuỗi. Bộ bảo vệ thuộc tính phân biệt đối xử thu hẹp loại

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

631 cho các loại cấu thành của

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

631 có thuộc tính phân biệt

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

148 với một trong các giá trị có thể là

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

149.

Lưu ý rằng chúng tôi hiện chỉ hỗ trợ các thuộc tính phân biệt của các loại theo nghĩa đen. Chúng tôi dự định sau này thêm hỗ trợ cho các loại boolean và số bằng chữ.

Loại tsinterface Options { /** File patterns to be excluded. */ exclude?: string[]; /** * It handles any extra properties that we haven't declared as type 'any'. */ [x: string]: any;}function processOptions(opts: Options) { // Notice we're *intentionally* accessing `excludes`, not `exclude` if (opts.excludes) { console.error( "The option `excludes` is not valid. Did you mean `exclude`?" ); }}13

TypeScript 2.0 giới thiệu một loại nguyên thủy mới

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13. Loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13 đại diện cho loại giá trị không bao giờ xảy ra. Cụ thể,

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13 là loại trả về cho các chức năng không bao giờ trả lại và

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13 là loại biến trong các bộ bảo vệ loại không bao giờ đúng.

Loại

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13 có các đặc điểm sau:

  • ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    13 là một loại phụ và có thể gán cho mọi loại.
  • Không có loại là một kiểu con hoặc được gán cho

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    13 (ngoại trừ chính

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    13).
  • Trong một biểu thức chức năng hoặc hàm mũi tên không có chú thích loại trả về, nếu hàm không có câu lệnh

    ts

    abstract class Shape {

    abstract getArea(): number;

    }

    // ---cut---

    interface HasArea {

    getArea(): number;

    }

    // Works!

    let Ctor: abstract new () => HasArea = Shape;

    39 hoặc chỉ các câu lệnh

    ts

    abstract class Shape {

    abstract getArea(): number;

    }

    // ---cut---

    interface HasArea {

    getArea(): number;

    }

    // Works!

    let Ctor: abstract new () => HasArea = Shape;

    39 với các biểu thức của loại

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    13 và nếu điểm cuối của hàm không thể truy cập được (như được xác định bởi phân tích luồng điều khiển) , loại trả về suy luận cho hàm là

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    13.
  • Trong một hàm có chú thích loại trả về

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    13 rõ ràng, tất cả các câu lệnh

    ts

    abstract class Shape {

    abstract getArea(): number;

    }

    // ---cut---

    interface HasArea {

    getArea(): number;

    }

    // Works!

    let Ctor: abstract new () => HasArea = Shape;

    39 (nếu có) phải có biểu thức của loại

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    13 và điểm cuối của hàm không thể tiếp cận được.

Bởi vì

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13 là một kiểu con của mọi loại, nó luôn được bỏ qua khỏi các loại liên minh và nó bị bỏ qua trong suy luận loại trả về chức năng miễn là có các loại khác được trả về.

Một số ví dụ về các chức năng trả về

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13:

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

04

Một số ví dụ về việc sử dụng các chức năng trả về

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13:

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

05

Bởi vì

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13 có thể được gán cho mọi loại, một hàm trả về

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

13 có thể được sử dụng khi một cuộc gọi lại trả về một loại cụ thể hơn là bắt buộc:

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

06

Thuộc tính chỉ đọc và chữ ký chỉ mục

Một chữ ký thuộc tính hoặc chỉ số hiện có thể được khai báo với công cụ sửa đổi

ts

// A tuple that stores a pair of numbers

let a: [number, number] = [1, 2];

// A tuple that stores a string, a number, and a boolean

let b: [string, number, boolean] = ["hello", 42, true];

118 được coi là chỉ đọc.

Các thuộc tính chỉ đọc có thể có bộ khởi tạo và có thể được gán cho các hàm tạo trong cùng một khai báo lớp, nhưng nếu không thì các bài tập cho các thuộc tính chỉ đọc không được phép.

Ngoài ra, các thực thể chỉ được đọc ngầm chỉ trong một số tình huống:

  • Một tài sản được khai báo với người truy cập

    ts

    abstract class Shape {

    abstract getArea(): number;

    }

    // ---cut---

    interface HasArea {

    getArea(): number;

    }

    // Works!

    let Ctor: abstract new () => HasArea = Shape;

    93 và không có người truy cập

    ts

    abstract class Shape {

    abstract getArea(): number;

    }

    // ---cut---

    interface HasArea {

    getArea(): number;

    }

    // Works!

    let Ctor: abstract new () => HasArea = Shape;

    94 được coi là chỉ đọc.
  • Trong loại đối tượng enum, các thành viên enum được coi là thuộc tính chỉ đọc.
  • Trong loại của một đối tượng mô-đun, các biến

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    178 được xuất được coi là thuộc tính chỉ đọc.
  • Một thực thể được tuyên bố trong một tuyên bố

    ts

    abstract class SuperClass {

    abstract someMethod(): void;

    badda() {}

    }

    type AbstractConstructor<T> = abstract new (...args: any[]) => T

    function withStyles<T extends AbstractConstructor<object>>(Ctor: T) {

    abstract class StyledClass extends Ctor {

    getStyles() {

    // ...

    }

    }

    return StyledClass;

    }

    class SubClass extends withStyles(SuperClass) {

    someMethod() {

    this.someMethod()

    }

    }

    51 được coi là chỉ đọc.
  • Một thực thể được truy cập thông qua nhập không gian tên ES2015 được coi là chỉ đọc (ví dụ:

    ts

    let foo: [...string[], number];

    foo = [123];

    foo = ["hello", 123];

    foo = ["hello!", "hello!", "hello!", 123];

    let bar: [boolean, ...string[], boolean];

    bar = [true, false];

    bar = [true, "some text", false];

    bar = [true, "some", "separated", "text", false];

    180 chỉ được đọc khi

    ts

    interface Options {

    /** File patterns to be excluded. */

    exclude?: string[];

    /**

    * It handles any extra properties that we haven't declared as type 'any'.

    */

    [x: string]: any;

    }

    function processOptions(opts: Options) {

    // Notice we're *intentionally* accessing `excludes`, not `exclude`

    if (opts.excludes) {

    console.error(

    "The option `excludes` is not valid. Did you mean `exclude`?"

    );

    }

    }

    70 được khai báo là

    ts

    // A tuple that stores a pair of numbers

    let a: [number, number] = [1, 2];

    // A tuple that stores a string, a number, and a boolean

    let b: [string, number, boolean] = ["hello", 42, true];

    662).

Thí dụ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

07

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

08

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

09

Chỉ định loại tslet foo: [...string[], number];foo = [123];foo = ["hello", 123];foo = ["hello!", "hello!", "hello!", 123];let bar: [boolean, ...string[], boolean];bar = [true, false];bar = [true, "some text", false];bar = [true, "some", "separated", "text", false];036 cho các chức năng

Theo dõi việc chỉ định loại

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 trong một lớp hoặc giao diện, các chức năng và phương thức hiện có thể khai báo loại

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 mà họ mong đợi.

Theo mặc định, loại

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 bên trong một hàm là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

72. Bắt đầu với TypeScript 2.0, bạn có thể cung cấp một tham số

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 rõ ràng.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 tham số là các tham số giả mạo xuất hiện đầu tiên trong danh sách tham số của một hàm:

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

10

tslet foo: [...string[], number];foo = [123];foo = ["hello", 123];foo = ["hello!", "hello!", "hello!", 123];let bar: [boolean, ...string[], boolean];bar = [true, false];bar = [true, "some text", false];bar = [true, "some", "separated", "text", false];036 tham số trong cuộc gọi lại

Các thư viện cũng có thể sử dụng các tham số

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 để khai báo các cuộc gọi lại sẽ được gọi.

Thí dụ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

11

Chỉ định loại

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 cho các chức năng

Theo dõi việc chỉ định loại

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 trong một lớp hoặc giao diện, các chức năng và phương thức hiện có thể khai báo loại

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 mà họ mong đợi.

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

12

tslet foo: [...string[], number];foo = [123];foo = ["hello", 123];foo = ["hello!", "hello!", "hello!", 123];let bar: [boolean, ...string[], boolean];bar = [true, false];bar = [true, "some text", false];bar = [true, "some", "separated", "text", false];197

Theo mặc định, loại

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 bên trong một hàm là

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

72. Bắt đầu với TypeScript 2.0, bạn có thể cung cấp một tham số

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 rõ ràng.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 tham số là các tham số giả mạo xuất hiện đầu tiên trong danh sách tham số của một hàm:

tslet foo: [...string[], number];foo = [123];foo = ["hello", 123];foo = ["hello!", "hello!", "hello!", 123];let bar: [boolean, ...string[], boolean];bar = [true, false];bar = [true, "some text", false];bar = [true, "some", "separated", "text", false];036 tham số trong cuộc gọi lại

Các thư viện cũng có thể sử dụng các tham số

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 để khai báo các cuộc gọi lại sẽ được gọi.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

192 có nghĩa là

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

193 dự kiến ​​

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

194 sẽ là một hàm không yêu cầu loại

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036.

Thí dụ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

13

Chỉ định loại

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 cho các chức năng

  • Theo dõi việc chỉ định loại

    ts

    let foo: [...string[], number];

    foo = [123];

    foo = ["hello", 123];

    foo = ["hello!", "hello!", "hello!", 123];

    let bar: [boolean, ...string[], boolean];

    bar = [true, false];

    bar = [true, "some text", false];

    bar = [true, "some", "separated", "text", false];

    036 trong một lớp hoặc giao diện, các chức năng và phương thức hiện có thể khai báo loại

    ts

    let foo: [...string[], number];

    foo = [123];

    foo = ["hello", 123];

    foo = ["hello!", "hello!", "hello!", 123];

    let bar: [boolean, ...string[], boolean];

    bar = [true, false];

    bar = [true, "some text", false];

    bar = [true, "some", "separated", "text", false];

    036 mà họ mong đợi.
  • Theo mặc định, loại

    ts

    let foo: [...string[], number];

    foo = [123];

    foo = ["hello", 123];

    foo = ["hello!", "hello!", "hello!", 123];

    let bar: [boolean, ...string[], boolean];

    bar = [true, false];

    bar = [true, "some text", false];

    bar = [true, "some", "separated", "text", false];

    036 bên trong một hàm là

    ts

    interface SomeType {

    /** This is an index signature. */

    [propName: string]: any;

    }

    function doStuff(value: SomeType) {

    let x = value["someProperty"];

    }

    72. Bắt đầu với TypeScript 2.0, bạn có thể cung cấp một tham số

    ts

    let foo: [...string[], number];

    foo = [123];

    foo = ["hello", 123];

    foo = ["hello!", "hello!", "hello!", 123];

    let bar: [boolean, ...string[], boolean];

    bar = [true, false];

    bar = [true, "some text", false];

    bar = [true, "some", "separated", "text", false];

    036 rõ ràng.

    ts

    let foo: [...string[], number];

    foo = [123];

    foo = ["hello", 123];

    foo = ["hello!", "hello!", "hello!", 123];

    let bar: [boolean, ...string[], boolean];

    bar = [true, false];

    bar = [true, "some text", false];

    bar = [true, "some", "separated", "text", false];

    036 tham số là các tham số giả mạo xuất hiện đầu tiên trong danh sách tham số của một hàm:
  • ts

    let foo: [...string[], number];

    foo = [123];

    foo = ["hello", 123];

    foo = ["hello!", "hello!", "hello!", 123];

    let bar: [boolean, ...string[], boolean];

    bar = [true, false];

    bar = [true, "some text", false];

    bar = [true, "some", "separated", "text", false];

    036 tham số trong cuộc gọi lại

Các thư viện cũng có thể sử dụng các tham số

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036 để khai báo các cuộc gọi lại sẽ được gọi.

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

192 có nghĩa là

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

193 dự kiến ​​

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

194 sẽ là một hàm không yêu cầu loại

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

036.

Nếu các thuộc tính

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

052 hoặc

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

053 được chỉ định, thay vào đó, trình biên dịch sẽ bao gồm liên kết của các tệp được bao gồm bởi hai thuộc tính đó. Các tệp trong thư mục được chỉ định bằng tùy chọn trình biên dịch

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

224 luôn được loại trừ trừ khi bao gồm rõ ràng thông qua thuộc tính

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

052 (ngay cả khi thuộc tính

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

45 được chỉ định).

Các tệp được bao gồm bằng cách sử dụng

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

053 có thể được lọc bằng thuộc tính

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

45. Tuy nhiên, các tệp được bao gồm rõ ràng bằng cách sử dụng thuộc tính

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

052 luôn được bao gồm bất kể

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

45. Thuộc tính

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

45 mặc định là loại trừ các thư mục

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

44,

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

233 và

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

234 khi không được chỉ định.

Các cải tiến độ phân giải mô -đun: BaseURL, ánh xạ đường dẫn, rootDirs và theo dõi

TypeScript 2.0 cung cấp một tập hợp các knops độ phân giải mô -đun bổ sung để thông báo cho trình biên dịch nơi tìm khai báo cho một mô -đun nhất định.

Xem tài liệu độ phân giải mô -đun để biết thêm chi tiết.

URL cơ sở

Sử dụng

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

38 là một thông lệ phổ biến trong các ứng dụng sử dụng các trình tải mô-đun AMD trong đó các mô-đun được triển khai trên mạng trong một thư mục duy nhất trong thời gian chạy. Tất cả các mô-đun nhập khẩu với các tên không liên quan được giả định là liên quan đến

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

38.

Thí dụ

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

14

Bây giờ nhập vào

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

237 sẽ được tra cứu trong

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

238

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

15

Ánh xạ đường dẫn

Đôi khi các mô -đun không được đặt trực tiếp dưới baseurl. Trình tải sử dụng cấu hình ánh xạ để ánh xạ tên mô-đun vào các tệp tại thời gian chạy, xem Tài liệu yêu cầu và tài liệu SystemJS.

Trình biên dịch TypeScript hỗ trợ khai báo các ánh xạ đó bằng thuộc tính

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

37 trong các tệp

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98.

Thí dụ

Bây giờ nhập vào

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

237 sẽ được tra cứu trong

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

238

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

16

Ánh xạ đường dẫn

Đôi khi các mô -đun không được đặt trực tiếp dưới baseurl. Trình tải sử dụng cấu hình ánh xạ để ánh xạ tên mô-đun vào các tệp tại thời gian chạy, xem Tài liệu yêu cầu và tài liệu SystemJS.

Trình biên dịch TypeScript hỗ trợ khai báo các ánh xạ đó bằng thuộc tính

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

37 trong các tệp

ts

interface Options {

/** File patterns to be excluded. */

exclude?: string[];

/**

* It handles any extra properties that we haven't declared as type 'any'.

*/

[x: string]: any;

}

function processOptions(opts: Options) {

// Notice we're *intentionally* accessing `excludes`, not `exclude`

if (opts.excludes) {

console.error(

"The option `excludes` is not valid. Did you mean `exclude`?"

);

}

}

98.

Thí dụ

Bây giờ nhập vào

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

237 sẽ được tra cứu trong

ts

let foo: [...string[], number];

foo = [123];

foo = ["hello", 123];

foo = ["hello!", "hello!", "hello!", 123];

let bar: [boolean, ...string[], boolean];

bar = [true, false];

bar = [true, "some text", false];

bar = [true, "some", "separated", "text", false];

238

ts

interface SomeType {

/** This is an index signature. */

[propName: string]: any;

}

function doStuff(value: SomeType) {

let x = value["someProperty"];

}

17

Ánh xạ đường dẫn