Skip to content

Latest commit

 

History

History
82 lines (52 loc) · 1.74 KB

File metadata and controls

82 lines (52 loc) · 1.74 KB

Type Assertions, Type Casting and Type Declaration



Type Assertions

Type Casting or Type Coercion

This is done by JS (not a TS feature)

let value: string | number = '123';

value = +value;

console.log(typeof value); // number

Type Declarations

We don't provide implementation within declarations.

If we try to access the property PORT from the env. variables of our runtime

const user = process.env.PORT;

TS will error with:

Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

We can install @types/node or declare the type:

declare const process: any;

const user = process.env.PORT;

We can move the declarations to their own file. The naming convention is: file-name.d.ts Example: process.d.ts

Always prefer "types packages" (example: @types/node) to creating you owns. Yet, it can be useful for libraries that don't have their declarations.

You can search for types packages here: https://github.com/DefinitelyTyped/DefinitelyTyped

Note: today, most of the projects support TS so you don't have to install a "type package".