TypeScript
Learn to become a modern TypeScript developer by following the steps, skills, resources and guides listed in this pack.
Learn to become a modern TypeScript developer by following the steps, skills, resources and guides listed in this pack.
// @ts-check comment at the top of a file. This allows the compiler to validate types by inspecting the JSDoc comments:target: the version of JavaScript to compile to.
module: the module system to use.
strict: enables/disables strict type checking.
outDir: the directory to output the compiled JavaScript files.
rootDir: the root directory of the TypeScript files.
include: an array of file/directory patterns to include in the compilation.
exclude: an array of file/directory patterns to exclude from the compilation.
Given below is the sample tsconfig.json file:-- prefix, for example:.ts file (e.g. app.ts)
Compile the TypeScript code into JavaScript using the TypeScript compiler:tsc is the command line tool for the TypeScript compiler. It compiles TypeScript code into JavaScript code, making it compatible with the browser or any JavaScript runtime environment.
You can use the tsc command to compile your TypeScript code by running the following command in your terminal or command prompt:boolean is a primitive data type in TypeScript that represents a boolean value i.e. either true or false. Given below is an example of a boolean variable declaration:void represents the return value of functions which don’t return a value. It’s the inferred type any time a function doesn’t have any return statements, or doesn’t return any explicit value from those return statements:null (absent) and undefined (uninitialized).
TypeScript has two corresponding types by the same names. How these types behave depends on whether you have the strictNullChecks option on.
With strictNullChecks off, values that might be null or undefined can still be accessed normally, and the values null and undefined can be assigned to a property of any type. This is similar to how languages without null checks (e.g. C#, Java) behave. The lack of checking for these values tends to be a major source of bugs; TypeScript always recommend people turn strictNullChecks on if it’s practical to do so in the codebase.
With strictNullChecks on, when a value is null or undefined, you will need to test for those values before using methods or properties on that value. Just like checking for undefined before using an optional property, we can use narrowing to check for values that might be null:null (absent) and undefined (unintialized).
TypeScript has two corresponding types by the same names. How these types behave depends on whether you have the strictNullChecks option on.
With strictNullChecks off, values that might be null or undefined can still be accessed normally, and the values null and undefined can be assigned to a property of any type. This is similar to how languages without null checks (e.g. C#, Java) behave. The lack of checking for these values tends to be a major source of bugs; TypeScript always recommend people turn strictNullChecks on if it’s practical to do so in the codebase.
With strictNullChecks on, when a value is null or undefined, you will need to test for those values before using methods or properties on that value. Just like checking for undefined before using an optional property, we can use narrowing to check for values that might be null:[1, 2, 3], you can use the syntax number[]; this syntax works for any type (e.g. string[] is an array of strings, and so on). You may also see this written as Array<number>, which means the same thing.object type, we simply list its properties and their types.
For example, here’s a function that takes a point-like object:unknown is the type-safe counterpart of any. Anything is assignable to unknown, but unknown isn’t assignable to anything but itself and any without a type assertion or a control flow based narrowing. Likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type.any, that you can use whenever you don’t want a particular value to cause typechecking errors.
When a value is of type any, you can access any properties of it (which will in turn be of type any), call it like a function, assign it to (or from) a value of any type, or pretty much anything else that’s syntactically legal:never type represents the type of values that never occur. For instance, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns. Variables also acquire the type never when narrowed by any type guards that can never be true.
The never type is a subtype of, and assignable to, every type; however, no type is a subtype of, or assignable to, never (except never itself). Even any isn’t assignable to never.
Examples of functions returning never:as const is a type assertion in TypeScript that allows you to assert that an expression has a specific type, and that its value should be treated as a read-only value.
For example:any is a special type in TypeScript that represents a value of any type. When a value is declared with the any type, the compiler will not perform any type checks or type inference on that value.
For example:satisfies operator lets us validate that the type of an expression matches some type, without changing the resulting type of that expression.
Learn more from the following resources:
| is used to combine two or more types into a single type that represents all the possible types. For example:| separated list of types.
For example, consider a function that takes either a string or a number as an argument:& operator as follows:keyof operator in TypeScript is used to get the union of keys from an object type. Here’s an example of how it can be used:instanceof operator is a way to narrow down the type of a variable. It is used to check if an object is an instance of a class.typeof operator is used to check the type of a variable. It returns a string value representing the type of the variable.===, !==, ==, and != to narrow types. For example:&&s, ||s, if statements, Boolean negations (!), and more. As an example, if statements don’t expect their condition to always have the type boolean.interface in TypeScript is a blueprint for creating objects with specific structure. An interface defines a set of properties, methods, and events that a class or object must implement. The interface is a contract between objects and classes and can be used to enforce a specific structure for objects in your code.
Here is an example of an interface declaration in TypeScript:public, private, protected) and/or type annotations. The parameters are then automatically assigned to properties of the same name within the constructor, and can be accessed within the class. For example:public: This is the default access modifier. Properties and methods declared as public can be accessed from anywhere, both inside and outside the class.
private: Properties and methods declared as private can only be accessed within the same class. They are not accessible from outside the class.
protected: Properties and methods declared as protected can be accessed within the class and its subclasses. They are not accessible from outside the class and its subclasses.
Access modifiers in TypeScript allow you to define the level of visibility and accessibility of properties and methods in your class, making your code more maintainable and secure.
Learn more from the following resources:
<T> and can be used as a placeholder for a specific data type. The actual data type is specified when the function or class is used.
For example, the following is a generic function that takes a single argument of any data type and returns the same data type:extends keyword, followed by the type that the type parameter must extend or implement.Partial: makes all properties of a type optional.
Readonly: makes all properties of a type read-only.
Pick: allows you to pick specific properties from a type.
Omit: allows you to omit specific properties from a type.
Exclude: creates a type that is the set difference of A and B.
..and more.
Learn more from the following links:
null and undefined from Type..then() method on Promises - specifically, the way that they recursively unwrap Promises.keyof operator and a type that maps each property of the existing type to a new property type.
For example, the following is a mapped type that takes an object type and creates a new type with all properties of the original type but with their type changed to readonly:infer keyword and a type that tests a condition and selects a type based on the result of the test.
For example, the following is a conditional type that takes two types and returns the type of the first argument if it extends the second argument, and the type of the second argument otherwise: