TypeScript: Getting Started With TSConfig Options
What is TypeScript?
TypeScript is a superset of JavaScript. This means you can use JavaScript syntax in TypeScript, so learning JavaScript before jumping into TypeScript is best. With that disclaimer, let’s talk about what TypeScript is all about and how we can set up a project for TypeScript.
According to the documentation for TypeScript, “TypeScript is a strongly typed programming language that builds on JavaScript.”[1] The TypeScript language adds type syntax to help you catch errors early during development and keeps safeguards up while your development team grows as a project scales. TypeScript allows you to define the datatypes for variables and interfaces for objects. TypeScript uses these definitions to check your code for errors as it compiles and informs you when you are not following the definitions you set beforehand.
If you're thinking, "Why do I need to write more code just to use TypeScript when I can be more careful in the first place?" You're not wrong, but humans can make errors, and we often do when working for long periods. If this same thinking were applied to construction, then the additional scaffolding and safety procedures would require more work and time, and we can save time by just being careful at the work site.
See? In an effort to save time, this roofer may have wasted more time and resources. This is just one worker. Imagine a team of workers working on a project as more team members are added. This is the reality of software development; having a plan to catch these mistakes will help you and your team in the long run.
TypeScript Examples
The following examples come from the TypeScript documentation[2], but the commentary is mine.
TypeScript:
function greet(person, date) { console.log(`Hello ${person}, today is ${date}!`); } greet("Brendan");
TypeScript will catch the error when greet is called. We defined greet to receive two arguments, person and date, and we only supplied person. TypeScript will catch this error when it compiles the code and lets you know it expected a second argument. In a way, TypeScript can be considered a linter for your code to catch these errors while you're working, but we can leverage the type syntax to help us further.
function greet(person: string, date: Date) { console.log(`Hello ${person}, today is ${date.toDateString()}!`); } greet("Maddison", Date());
Now, we add a type to the two arguments, person must be a string, and date must be a Date object for the toDateString method. When greet is called without the keyword new before the second argument, Date(), TypeScript will let you know it received a string instead of a Date for the date parameter. Now, you can fix your error before developing further and having to trace back to this error when you receive an unexpected output while testing.
Now that you've seen TypeScript in action let's examine the next steps in setting up your project to use it.
Configuring TypeScript's Compiler: tsconfig.json
The default TypeScript compiling may not be what you are looking for, and there is a way to customize TypeScript for your needs, similar to using a linter, but it can do so much more.
The first step is to create a tsconfig.json file in your project's root directory. This file tells TypeScript which files should be included in the compilation process. In the tsconfig.json, you can specify the directories from the root that should be included if you want more specificity using your JSON file's "includes" key.
function greet(person, date) { console.log(`Hello ${person}, today is ${date}!`); } greet("Brendan");
Now, let's talk about "compilerOptions". Trust me when I say there are a ton of options to choose from. This is a good thing but also terrifying if this is your first time using TypeScript. I'm going to break down a few of the popular choices to help you get started:
allowJs
function greet(person: string, date: Date) { console.log(`Hello ${person}, today is ${date.toDateString()}!`); } greet("Maddison", Date());
This option allows JavaScript files to be imported inside your project instead of only TypeScript. Typically, TypeScript assumes all imports are TypeScript and would give an error for imported JavaScript files, but this option allows us to use those imports and can be helpful when working with TypeScript and JavaScript in the same repository.
esModuleInterop
{ "include": ["src/**/*"] }
A namespace import in ES6 can only be an object, but since this is the same as using require without .default, we have allowed TypeScript to treat an object as a function. Instead of having to be careful about our imports, this option will fix this issue when TypeScript transpiles the code into JavaScript.
target
{ "compilerOptions": { "allowJs": true } }
This option changes which JS features are downgraded and which are left intact when TypeScript transpiles your code into JavaScript. es6 is a good choice since most modern browsers support ES6, but you can designate any version of ECMAScript for this option to suit your needs.
strict
{ "compilerOptions": { "esModuleInterop": true } }
This flag enables many different type checking behaviors. This will result in a stronger codebase with fewer errors. If you like to exclude certain type checking behaviors, check the documentation [3] and set their options to false. If you only want a couple type checking behaviors, I would consider turn those on instead of using strict.
outDir
{ "compilerOptions": { "target": "es6" } }
When TypeScript transpiles your code into usable JavaScript, this option will emit those files into this directory.
noEmit
function greet(person, date) { console.log(`Hello ${person}, today is ${date}!`); } greet("Brendan");
This option stops all transpiled JavaScript files from emitting. This may sound silly since I just told you about outDir, but TypeScript will always emit files and outDir will direct the files to the correct location. noEmit comes into play when you are already using another tool like Babel or Webpack to transpile and bundle your code. Emitting in this case would mean creating useless JavaScript files.
Conclusion
There you have it. Those are seven options to help you set up your configuration for TypeScript and how TypeScript can help you create a more stable code base. I recommend giving Matt Pocock's "The TSConfig Cheat Sheet" a read for more popular options to put in your tsconfig.json and always refer to the TypeScript documentation on the TSConfig before implementing any of these options.
Happy Coding!
Tyler Meyer
Sources:
[1] https://www.typescriptlang.org/
[2] https://www.typescriptlang.org/docs/handbook/2/basic-types.html
[3] https://www.typescriptlang.org/tsconfig/
[4] https://www.totaltypescript.com/tsconfig-cheat-sheet
The above is the detailed content of TypeScript: Getting Started With TSConfig Options. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
