In this article, i'll share with you some Tips for using TypeScript. Most of the examples are demonstrated in the example of a React application.
TypeScript (TS) is a superset of JavaScript (JS) syntax that is optionally typed and compiled (using TSC or Babel) to plain JavaScript. Technically, TS is statically typed JS.
“Adopting TypeScript is not a binary choice, you can start by annotating existing JavaScript with JSDoc, then switch a few files to be checked by TypeScript and over time prepare your codebase to convert completely.”
Once compiled, the program can be run in any modern browser or used on a server (Node.js or Deno).
Always prefer to specify the types of variables, parameters, and return valuesto use the full power of TypeScript. This makes future refactoring easier. Most of the examples are demonstrated in the example of a React application.
There are two main ways to compile ts files: original TSC and @babel. The babel method is interesting in that information about types is stripped out from .ts files, due to which the assembly is fast.
Create prettier imports by defining paths and baseUrl properties to compilerOptions in tsconfig.json
This will avoid long relative paths when importing.
First opportunity. You can disallow the use of any using the --noImplicitAny key for the tsc compiler, or add the noImplicitAny property to compilerOptions in tsconfig.json.
Second possibility to use alias:
Why do we use any?
Sometimes it's better to use unknown instead of any
never is a good insurance against unprocessed cases.
never in React: for example, you can prevent nesting or passing props:
Use types when you need union or intersection. Use an interface whenever you want to use extends or implements. However, there is no hard and fast rule, use what works for you.
TypeScript 3.2 (Nov 2018)
Since we have ES6 modules as a standard in JavaScript, we don’t need custom TypeScript modules and namespaces. Instead, we should use standard JavaScript modules with import and export instead.
do not do this:
do this:
When might you need it?
Typings are usually located in a separate directory typings/index.d.ts. You can connect them in tsconfig.json
TypeScript supports the syntax of getters and setters. Using getters and setters to access object data is much better than accessing its properties directly.
Why?
with enum:
with literal type:
both:
In this example, both components are checked for the color type. The enum version turned out to be more redundant - this is additional code during compilation, and additional imports in the source code. The string literal variant is more short and also contains type checking. There is no point in complicating the enum code.
If you need to support runtime enums use following pattern:
as const tells the compiler that the given object will not change. Hence, you can deduce the types in more detail.
You can also apply as const to arrays:
Brand is the small utility for creating unique ("branded") types. Usually such types are created on the basis of primitive ones - strings, numbers.
This creates a union of types with a common part. When working with such a union, the ts compiler can filter out variants and concretize the type.
The type system in TypeScript allows you to mark individual properties of an interface/class as readonly fields. For more complex scenarios, there is a built-in type Readonly, which takes a type T and marks all of its read-only properties using mapped types (see mapped types).
Using freezing on initialState and defaultProps, the type system will infer the correct readonly types.
Why use type?
Using TypeScript in a project changes the way we write code. It becomes easier to understand refactoring the code. You do not need to keep in mind the implementation details.
With new advantages come new challenges - maintaining the correct type system.
I hope these tips will help you to improve your system. Of course, this is not a complete list, but it can serve as a good starting point.