🫢Cấu hình này rất quan trọng "lib" nó giúp chạy js trực tiếp trong trình duyệt (ok) 😍

https://stackoverflow.com/questions/50986494/what-does-the-typescript-lib-option-really-do

Hãy nhớ rằng, TS không bao giờ chèn polyfill độn vào mã của bạn. Đó không phải là mục tiêu của nó.

Target cho TS biết thông số kỹ thuật ES nào bạn muốn mã cuối cùng/mã biên dịch hỗ trợ. Nếu bạn định cấu hình nó là ES5, TS sẽ biên dịch xuống các tính năng cú pháp thành ES5, do đó bất kỳ hàm mũi tên nào () => {} trong mã của bạn sẽ được chuyển đổi thành hàm () {}.

Bất cứ điều gì bạn chọn cho mục tiêu đều ảnh hưởng đến giá trị mặc định của lib, giá trị này sẽ cho TS biết định nghĩa kiểu nào cần đưa vào dự án của bạn. Nếu bạn có "target": "es5", giá trị mặc định của lib sẽ là ["dom", "es5", "ScriptHost"]. Giá trị này giả định các tính năng chức năng nào mà trình duyệt sẽ hỗ trợ khi chạy. Việc thêm các thứ vào lib chỉ để làm TS hài lòng - bạn vẫn cần tự nhập polyfill vào dự án.

Bất cứ điều gì bạn chọn cho mục tiêu đều ảnh hưởng đến giá trị mặc định của lib, giá trị này sẽ cho TS biết định nghĩa kiểu nào cần đưa vào dự án của bạn. Nếu bạn có "target": "es5", giá trị mặc định của lib sẽ là ["dom", "es5", "ScriptHost"]. Giá trị này giả định các tính năng chức năng nào mà trình duyệt sẽ hỗ trợ khi chạy. Việc thêm các thứ vào lib chỉ để làm TS hài lòng - bạn vẫn cần tự nhập polyfill vào dự án.

Ví dụ: Bạn cần hỗ trợ IE11 nhưng bạn cũng muốn sử dụng promises. IE11 hỗ trợ ES5, nhưng promises là một tính năng của ES6. Bạn nhập polyfill promises, nhưng TS vẫn báo lỗi. Bây giờ bạn chỉ cần cho TypeScript biết rằng mã của bạn sẽ nhắm mục tiêu đến ES5 và có thể sử dụng promises trong cơ sở mã một cách an toàn:

"target": "es5",
"lib": ["dom", "es5", "ScriptHost", "es2015.promise"]

Typescript không có bất kỳ kiểu tích hợp nào, tất cả các kiểu đều xuất phát từ một tập hợp các định nghĩa cơ sở (nằm trong thư mục lib trong thư mục cài đặt Typescript). Theo mặc định, mục tiêu xác định các lib nào được bao gồm. Ví dụ, tài liệu nêu:

Lưu ý: Nếu --lib không được chỉ định, danh sách librare mặc định sẽ được đưa vào. Các thư viện mặc định được đưa vào là:

--target ES5: DOM,ES5,ScriptHost
--target ES6: DOM,ES6,DOM.Iterable,ScriptHost

Ý tưởng cơ bản là trong khi mục tiêu xử lý các tính năng ngôn ngữ (cụ thể hơn là các tính năng ngôn ngữ nào cần được biên dịch xuống, ví dụ: for-of hoặc các hàm mũi tên), thì tùy chọn lib xử lý các tiện ích mà môi trường thời gian chạy có (tức là các đối tượng tích hợp trông như thế nào, chúng là gì).

Lý tưởng nhất là nên sử dụng các thư viện mặc định cho một mục tiêu nhất định. Tuy nhiên, chúng ta có thể có một môi trường hỗ trợ một số tiện ích thời gian chạy nhưng không hỗ trợ các tính năng ngôn ngữ hoặc chúng ta có thể nhắm mục tiêu thời gian chạy với phiên bản es thấp hơn và poly-fill một số tiện ích thời gian chạy, điều này thường có thể thực hiện được đối với một số thứ (ví dụ: Promises).

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "lib": [
      "es2015",
      "dom",
      "scripthost"
    ],
    "strict": true,
    "esModuleInterop": true
  }
}

app.ts

index.html

app.ts

app.js

What does the TypeScript "lib" option really do?

Ask QuestionAsked 6 years, 7 months agoModified 7 months agoViewed 10k timesReport this ad42

I have still not been able to find good answer for this. The "target" option defines, what version of Javascript the result will run on. The "lib" option is less clearly described anywhere. Seems like it is a more granular way to describe the target environment, but then it seems strange it affects what you can write in the .ts source files. Thought TS what as superset of JS, so why does it affect whether, e.g., Promise() is available or not? This seems like it does not only define the target but also affect what functions you have available in Typescript.

Can someone explain that clearly or direct to an answer (there is none at typescriptlang.org or in the books I have looked at). For instance "Specify library files to be included in the compilation", which explains absolutely nothing.

ShareImprove this questionFollowedited Jun 30, 2024 at 3:27Machavity's user avatarMachavity♦31.6k2727 gold badges9595 silver badges105105 bronze badgesasked Jun 22, 2018 at 11:01Morten H Pedersen's user avatarMorten H Pedersen86311 gold badge88 silver badges99 bronze badgesAdd a comment

2 Answers

Sorted by: Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 69

Remember, TS never injects polyfills in your code. It's not its goal. Complementing the accepted anwer:

target tells TS which ES specification you want the final/transpiled code to support. If you configure it as ES5, TS will down compile the syntactic features to ES5, so any arrow functions () => {} in your code will be transformed to function () {}.

Whatever you choose for target affects the default value of lib which in turn tells TS what type definitions to include in your project. If you have "target": "es5", the default value of lib will be ["dom", "es5", "ScriptHost"]. It's assuming which functional features the browser will support at runtime. Adding things to lib it's just to make TS happy - you still need to import the polyfill yourself in the project.

So in short: configure target first, and if you need any extra polyfill in your project OR you know your browser(s) will support this little extra feature, lib is how to make TS happy about it.

Example: You need to support IE11 but also you would like to use promises. IE11 supports ES5, but promises is an ES6 feature. You import a promises polyfill, but TS is still giving an error. Now you just need to tell TypeScript that your code will target ES5 and it's safe to use promises in the codebase:

ShareImprove this answerFollowedited Dec 14, 2021 at 17:36answered Aug 13, 2019 at 8:56cleison's user avatarcleison1,57311 gold badge1414 silver badges1515 bronze badges

Add a commentReport this ad20

Typescript does not have any built-in types all types come from a set of base definitions (located in the lib folder in the typescript install directory). By default the target defines which libs are included. For example the docs state:

Note: If --lib is not specified a default list of librares are injected. The default libraries injected are:

► For --target ES5: DOM,ES5,ScriptHost

► For --target ES6: DOM,ES6,DOM.Iterable,ScriptHost

The basic idea is that while target is deals with language features (more specifically which language features need to be down compiled, ex: for-of, or arrow functions), the lib option deals with what facilities the runtime environment has (ie. what built-in objects look like, what they are).

Ideally the default libs for a given target should be used. We may, however, have an environment which supports some of the runtime facilities but not the language features, or we may target runtime with a lower es version and poly-fill some of the runtime facilities, which can be in general done for some things (ex: Promises).

ShareImprove this answerFollowedited Jun 20, 2020 at 9:12Community's user avatarCommunityBot111 silver badgeanswered Jun 22, 2018 at 12:02Titian Cernicova-Dragomir's user avatarTitian Cernicova-Dragomir249k3737 gold badges458458 silver badges388388 bronze badges

Last updated

Was this helpful?