Thẻ Html trong chuỗi mẫu

Chúng có cùng cú pháp như các chuỗi ký tự mẫu trong JavaScript, nhưng được sử dụng ở các vị trí loại. Khi được sử dụng với các loại chữ cụ thể, một chữ mẫu tạo ra một loại chữ chuỗi mới bằng cách nối các nội dung

ts

type World = "world";

 

type Greeting = `hello ${World}`;

type Greeting = "hello world"

Try

Khi một liên kết được sử dụng ở vị trí được nội suy, loại là tập hợp của mọi chuỗi ký tự có thể có thể được đại diện bởi mỗi thành viên liên minh

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

Đối với mỗi vị trí được nội suy trong chữ mẫu, các liên kết được nhân chéo

ts

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type Lang = "en" | "ja" | "pt";

 

type LocaleMessageIDs = `${Lang}_${AllLocaleIDs}`;

type LocaleMessageIDs = "en_welcome_email_id" | "en_email_heading_id" | "en_footer_title_id" | "en_footer_sendoff_id" | "ja_welcome_email_id" | "ja_email_heading_id" | "ja_footer_title_id" | "ja_footer_sendoff_id" | "pt_welcome_email_id" | "pt_email_heading_id" | "pt_footer_title_id" | "pt_footer_sendoff_id"

Try

Chúng tôi thường khuyên mọi người nên sử dụng tạo trước thời hạn cho các hiệp hội chuỗi lớn, nhưng điều này hữu ích trong các trường hợp nhỏ hơn

Liên kết chuỗi trong các loại

Sức mạnh trong chữ mẫu xuất hiện khi xác định một chuỗi mới dựa trên thông tin bên trong một loại

Xem xét trường hợp một hàm [_______13] thêm một hàm mới gọi là ____14 vào một đối tượng được truyền. Trong JavaScript, cuộc gọi của nó có thể giống như.

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

5. Chúng ta có thể tưởng tượng đối tượng cơ sở trông giống như

ts

const passedObject = {

firstName: "Saoirse",

lastName: "Ronan",

age: 26,

};

Try

Hàm

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

6 sẽ được thêm vào đối tượng cơ sở cần có hai đối số, một

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

7 [a

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

8] và một

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

9 [a

ts

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type Lang = "en" | "ja" | "pt";

 

type LocaleMessageIDs = `${Lang}_${AllLocaleIDs}`;

type LocaleMessageIDs = "en_welcome_email_id" | "en_email_heading_id" | "en_footer_title_id" | "en_footer_sendoff_id" | "ja_welcome_email_id" | "ja_email_heading_id" | "ja_footer_title_id" | "ja_footer_sendoff_id" | "pt_welcome_email_id" | "pt_email_heading_id" | "pt_footer_title_id" | "pt_footer_sendoff_id"

Try

0]

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

7 phải ở dạng

ts

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type Lang = "en" | "ja" | "pt";

 

type LocaleMessageIDs = `${Lang}_${AllLocaleIDs}`;

type LocaleMessageIDs = "en_welcome_email_id" | "en_email_heading_id" | "en_footer_title_id" | "en_footer_sendoff_id" | "ja_welcome_email_id" | "ja_email_heading_id" | "ja_footer_title_id" | "ja_footer_sendoff_id" | "pt_welcome_email_id" | "pt_email_heading_id" | "pt_footer_title_id" | "pt_footer_sendoff_id"

Try

2;

Hàm

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

9, khi được gọi

  • Nên được chuyển một giá trị của loại được liên kết với tên

    ts

    type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

    type Lang = "en" | "ja" | "pt";

     

    type LocaleMessageIDs = `${Lang}_${AllLocaleIDs}`;

    type LocaleMessageIDs = "en_welcome_email_id" | "en_email_heading_id" | "en_footer_title_id" | "en_footer_sendoff_id" | "ja_welcome_email_id" | "ja_email_heading_id" | "ja_footer_title_id" | "ja_footer_sendoff_id" | "pt_welcome_email_id" | "pt_email_heading_id" | "pt_footer_title_id" | "pt_footer_sendoff_id"

    Try

    6; . Các sự kiện tương tự được liên kết với

    ts

    const passedObject = {

    firstName: "Saoirse",

    lastName: "Ronan",

    age: 26,

    };

    Try

    1 sẽ được gọi với đối số

    ts

    const passedObject = {

    firstName: "Saoirse",

    lastName: "Ronan",

    age: 26,

    };

    Try

    2
  • Nên có kiểu trả về

    ts

    const passedObject = {

    firstName: "Saoirse",

    lastName: "Ronan",

    age: 26,

    };

    Try

    3 [để minh họa đơn giản]

Do đó, chữ ký hàm ngây thơ của

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

4 có thể là.

ts

const passedObject = {

firstName: "Saoirse",

lastName: "Ronan",

age: 26,

};

Try

5. Tuy nhiên, trong phần mô tả trước, chúng tôi đã xác định các ràng buộc loại quan trọng mà chúng tôi muốn ghi lại trong mã của mình. Các kiểu chữ mẫu cho phép chúng tôi đưa các ràng buộc này vào mã của chúng tôi

ts

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type Lang = "en" | "ja" | "pt";

 

type LocaleMessageIDs = `${Lang}_${AllLocaleIDs}`;

type LocaleMessageIDs = "en_welcome_email_id" | "en_email_heading_id" | "en_footer_title_id" | "en_footer_sendoff_id" | "ja_welcome_email_id" | "ja_email_heading_id" | "ja_footer_title_id" | "ja_footer_sendoff_id" | "pt_welcome_email_id" | "pt_email_heading_id" | "pt_footer_title_id" | "pt_footer_sendoff_id"

Try

7

Lưu ý rằng

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

6 lắng nghe sự kiện

ts

const passedObject = {

firstName: "Saoirse",

lastName: "Ronan",

age: 26,

};

Try

7, không chỉ

ts

const passedObject = {

firstName: "Saoirse",

lastName: "Ronan",

age: 26,

};

Try

8. Thông số kỹ thuật ngây thơ của chúng tôi về

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

4 có thể trở nên mạnh mẽ hơn nếu chúng tôi đảm bảo rằng tập hợp các tên sự kiện đủ điều kiện bị hạn chế bởi sự kết hợp của các tên thuộc tính trong đối tượng được xem với "Đã thay đổi" được thêm vào cuối. Mặc dù chúng tôi cảm thấy thoải mái khi thực hiện phép tính như vậy trong JavaScript nhưng tôi. e.

ts

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type Lang = "en" | "ja" | "pt";

 

type LocaleMessageIDs = `${Lang}_${AllLocaleIDs}`;

type LocaleMessageIDs = "en_welcome_email_id" | "en_email_heading_id" | "en_footer_title_id" | "en_footer_sendoff_id" | "ja_welcome_email_id" | "ja_email_heading_id" | "ja_footer_title_id" | "ja_footer_sendoff_id" | "pt_welcome_email_id" | "pt_email_heading_id" | "pt_footer_title_id" | "pt_footer_sendoff_id"

Try

70, mẫu chữ bên trong hệ thống loại cung cấp một cách tiếp cận tương tự để thao tác chuỗi

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

3

Với điều này, chúng ta có thể xây dựng thứ gì đó bị lỗi khi đưa sai thuộc tính

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

4

Suy luận với Template Literals

Lưu ý rằng chúng tôi không được hưởng lợi từ tất cả thông tin được cung cấp trong đối tượng được truyền ban đầu. Với sự thay đổi của một

ts

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type Lang = "en" | "ja" | "pt";

 

type LocaleMessageIDs = `${Lang}_${AllLocaleIDs}`;

type LocaleMessageIDs = "en_welcome_email_id" | "en_email_heading_id" | "en_footer_title_id" | "en_footer_sendoff_id" | "ja_welcome_email_id" | "ja_email_heading_id" | "ja_footer_title_id" | "ja_footer_sendoff_id" | "pt_welcome_email_id" | "pt_email_heading_id" | "pt_footer_title_id" | "pt_footer_sendoff_id"

Try

4 [i. e. một sự kiện

ts

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type Lang = "en" | "ja" | "pt";

 

type LocaleMessageIDs = `${Lang}_${AllLocaleIDs}`;

type LocaleMessageIDs = "en_welcome_email_id" | "en_email_heading_id" | "en_footer_title_id" | "en_footer_sendoff_id" | "ja_welcome_email_id" | "ja_email_heading_id" | "ja_footer_title_id" | "ja_footer_sendoff_id" | "pt_welcome_email_id" | "pt_email_heading_id" | "pt_footer_title_id" | "pt_footer_sendoff_id"

Try

3], chúng ta nên mong đợi rằng cuộc gọi lại sẽ nhận được một đối số loại

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

8. Tương tự, cuộc gọi lại để thay đổi thành

ts

const passedObject = {

firstName: "Saoirse",

lastName: "Ronan",

age: 26,

};

Try

1 sẽ nhận được đối số

ts

const passedObject = {

firstName: "Saoirse",

lastName: "Ronan",

age: 26,

};

Try

2. Chúng ta đang ngây thơ sử dụng

ts

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type Lang = "en" | "ja" | "pt";

 

type LocaleMessageIDs = `${Lang}_${AllLocaleIDs}`;

type LocaleMessageIDs = "en_welcome_email_id" | "en_email_heading_id" | "en_footer_title_id" | "en_footer_sendoff_id" | "ja_welcome_email_id" | "ja_email_heading_id" | "ja_footer_title_id" | "ja_footer_sendoff_id" | "pt_welcome_email_id" | "pt_email_heading_id" | "pt_footer_title_id" | "pt_footer_sendoff_id"

Try

76 để nhập đối số của

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

9. Một lần nữa, các kiểu chữ mẫu giúp đảm bảo kiểu dữ liệu của một thuộc tính sẽ cùng kiểu với đối số đầu tiên của lệnh gọi lại của thuộc tính đó.

Cái nhìn sâu sắc quan trọng làm cho điều này có thể là điều này. chúng ta có thể sử dụng một hàm với một cái chung sao cho

  1. Chữ được sử dụng trong đối số đầu tiên được ghi dưới dạng chữ
  2. Kiểu chữ đó có thể được xác thực là nằm trong sự kết hợp của các thuộc tính hợp lệ trong kiểu chung
  3. Loại thuộc tính đã được xác thực có thể được tra cứu trong cấu trúc chung bằng cách sử dụng Truy cập được lập chỉ mục
  4. Thông tin đánh máy này sau đó có thể được áp dụng để đảm bảo đối số cho hàm gọi lại là cùng loại

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

2

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

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

6 thành một phương thức chung

Khi người dùng gọi với chuỗi

ts

const passedObject = {

firstName: "Saoirse",

lastName: "Ronan",

age: 26,

};

Try

7, TypeScript sẽ cố gắng suy ra đúng loại cho

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

30. Để làm điều đó, nó sẽ so sánh

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

30 với nội dung trước

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

32 và suy ra chuỗi

ts

const passedObject = {

firstName: "Saoirse",

lastName: "Ronan",

age: 26,

};

Try

8. Khi TypeScript tìm ra điều đó, phương thức

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

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

ts

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type Lang = "en" | "ja" | "pt";

 

type LocaleMessageIDs = `${Lang}_${AllLocaleIDs}`;

type LocaleMessageIDs = "en_welcome_email_id" | "en_email_heading_id" | "en_footer_title_id" | "en_footer_sendoff_id" | "ja_welcome_email_id" | "ja_email_heading_id" | "ja_footer_title_id" | "ja_footer_sendoff_id" | "pt_welcome_email_id" | "pt_email_heading_id" | "pt_footer_title_id" | "pt_footer_sendoff_id"

Try

4 trên đối tượng ban đầu, trong trường hợp này là

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

8. Tương tự, khi được gọi với

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

37, TypeScript sẽ tìm loại cho thuộc tính

ts

const passedObject = {

firstName: "Saoirse",

lastName: "Ronan",

age: 26,

};

Try

1 là

ts

const passedObject = {

firstName: "Saoirse",

lastName: "Ronan",

age: 26,

};

Try

2

Suy luận có thể được kết hợp theo nhiều cách khác nhau, thường là để giải cấu trúc các chuỗi và tái cấu trúc chúng theo những cách khác nhau

Các kiểu thao tác chuỗi nội tại

Để trợ giúp thao tác chuỗi, TypeScript bao gồm một tập hợp các loại có thể được sử dụng trong thao tác chuỗi. Các loại này được tích hợp sẵn vào trình biên dịch để thực hiện và không thể tìm thấy trong các tệp

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

40 đi kèm với TypeScript

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

41

Chuyển đổi từng ký tự trong chuỗi thành phiên bản chữ hoa

Ví dụ

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

7

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

42

Chuyển đổi từng ký tự trong chuỗi thành chữ thường tương đương

Ví dụ

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

9

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

43

Chuyển đổi ký tự đầu tiên trong chuỗi thành chữ hoa tương đương

Ví dụ

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

0

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

44

Chuyển đổi ký tự đầu tiên trong chuỗi thành chữ thường tương đương

Ví dụ

ts

type EmailLocaleIDs = "welcome_email" | "email_heading";

type FooterLocaleIDs = "footer_title" | "footer_sendoff";

 

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;

type AllLocaleIDs = "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"

Try

1Chi tiết kỹ thuật về các loại thao tác chuỗi nội tại

Mã, kể từ TypeScript 4. 1, đối với các hàm nội tại này, sử dụng trực tiếp các hàm thời gian chạy chuỗi JavaScript để thao tác và không nhận biết ngôn ngữ

Tôi có thể sử dụng ${} trong HTML không?

Cú pháp ${} cho phép chúng ta đặt một biểu thức vào đó và nó sẽ tạo ra giá trị , mà trong trường hợp của chúng ta ở trên chỉ là một biến chứa một chuỗi. Có điều cần lưu ý ở đây. nếu bạn muốn thêm các giá trị, như trên, bạn không cần sử dụng Chữ mẫu cho biến tên.

Làm cách nào để sử dụng HTML trong mẫu chữ?

Làm sạch HTML bằng văn bản mẫu được gắn thẻ . Chúng tôi muốn có hai loại đầu vào vào trình giữ chỗ, chuỗi văn bản thô cần được khử trùng và HTML an toàn do chúng tôi tạo hoặc đã được đưa qua trình khử trùng. in a template literal tag using a library like html-parser2. We want to have two types of input into the placeholder, raw text strings which needs sanitising and safe HTML which is either authored by us or has been put through the sanitiser.

Làm cách nào để sử dụng ${} trong JavaScript?

Phần giữ chỗ được biểu thị bằng ${} , với bất kỳ nội dung nào trong dấu ngoặc nhọn được coi là JavaScript và bất kỳ nội dung nào bên ngoài dấu ngoặc được coi là chuỗi . const method = 'interpolation' const dynamicString = `Chuỗi này đang sử dụng ${method}. `

`` trong JavaScript là gì?

Mặc dù dấu ngoặc đơn và dấu ngoặc kép là phổ biến nhất, nhưng chúng tôi có tùy chọn thứ 3 gọi là Backticks [ `` ]. Backticks là một tính năng ES6 cho phép bạn tạo chuỗi trong JavaScript . Mặc dù backticks chủ yếu được sử dụng cho mục đích nhúng mã hoặc HTML, nhưng chúng cũng hoạt động tương tự như dấu ngoặc đơn và dấu ngoặc kép.

Chủ Đề