Type Declarations

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Professional TypeScript Masterclass Lessons

1.Introduction
free
⏱️ 1:54
2.Setup
free
⏱️ 5:44
3.Primitive Types
free
⏱️ 1:42
4.Instance Types
free
⏱️ 1:52
5.Arrays And Tuples
free
⏱️ 1:38
6.Objects
free
⏱️ 1:33
7.const declarations
free
⏱️ 1:03
8.Function Types
free
⏱️ 1:57
9.Structural Typing
free
⏱️ 2:10
10.Classes in TypeScript
free
⏱️ 1:48
11.Target Compiler Option
free
⏱️ 2:37
12.Generics
⏱️ 3:02
13.Special Types any And unknown
⏱️ 2:00
14.JavaScript to TypeScript
⏱️ 1:32
15.Frontend Projects
⏱️ 3:49
16.Type Assertions
⏱️ 2:15
17.Type Casting
⏱️ 1:16
18.Modules
⏱️ 1:55
19.Type Declarations
⏱️ 4:25
20.Creating NPM packages
⏱️ 3:20
21.Async Await
⏱️ 3:05
22.Running in NodeJS
⏱️ 1:40
23.Lexical this
⏱️ 2:34
24.readonly Modifier
⏱️ 1:59
25.Union Types
⏱️ 2:57
26.Literal Types
⏱️ 2:58
27.Type Narrowing
⏱️ 4:19
28.Discriminated Unions
⏱️ 3:29
29.Class Parameter Properties
⏱️ 1:02
30.Strict Compiler Option
⏱️ 6:18
31.null vs undefined
⏱️ 4:19
32.Intersection Types
⏱️ 2:03
33.Optional Modifier
⏱️ 2:47
34.Non Null Assertion Operator
⏱️ 3:40
35.Interfaces
⏱️ 2:28
36.Interface Declaration Merging
⏱️ 1:01
37.Types vs Interfaces
⏱️ 2:16
38.never Type
⏱️ 3:00
39.implements Keyword
⏱️ 1:25
40.Definite Assignment Assertion
⏱️ 2:31
41.User Defined Type Guards
⏱️ 2:02
42.Assertion Functions
⏱️ 3:42
43.Function Overloading
⏱️ 4:15
44.Call Signatures
⏱️ 2:53
45.Abstract Classes
⏱️ 1:53
46.Index Signatures
⏱️ 3:08
47.Readonly Arrays and Tuples
⏱️ 2:58
48.Double Assertions
⏱️ 2:20
49.const Assertions
⏱️ 3:55
50.this Parameter
⏱️ 2:33
51.Generic Constraints
⏱️ 2:43
52.typeof Type Operator
⏱️ 2:12
53.Lookup Types
⏱️ 3:12
54.keyof Type Operator
⏱️ 3:55
55.Conditional Types
⏱️ 4:39
56.Contitional Types with Unions and never
⏱️ 3:32
57.infer Keyword and `ReturnType<T>`
⏱️ 3:47
58.Mapped Types
⏱️ 2:48
59.Mapped Type Modifiers
⏱️ 3:37
60.Template Literal Type
⏱️ 4:28
61.Partial<T>
⏱️ 1:27
62.Required<T>
⏱️ 1:36
63.Readonly<T>
⏱️ 1:34
64.Record<K, T>
⏱️ 4:05
65.Project References
⏱️ 4:18
66.undefined vs. optional
⏱️ 2:48
67.satisfies Operator
⏱️ 2:42
68.PropertyKey Type
⏱️ 0:57
69.ThisType<T>
⏱️ 4:11
70.Awaited<T>
⏱️ 4:12
71.String Manipulation Types
⏱️ 3:36
72.Mapped Types as Clauses
⏱️ 4:01
73.Union vs Intersection Mental Model
⏱️ 3:36
74.Enums are Bad
⏱️ 8:11

Type Declarations

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Creating a Declaration

For example for the process global present in NodeJS:

declare const process: any;

console.log(
'Logged in user:',
process.env.USER
);

Using @types packages

For example for NodeJS:

npm i @types/node

Now you have full access to safely use NodeJS built-in features e.g.

import fs from 'fs';
fs.writeFileSync('hello.txt', 'Hello world');

Using Third Party Packages

Install the package, and if it doesn't come with definitions built in, install its definitions from @types/:

npm i express
npm i @types/express

And now you can use the package safely e.g. a simple HTTP server:

import express from 'express';
const app = express();
app.get('/', function (req, res) {
res.send('Hello World');
});
app.listen(3000, () => {
console.log('Server started');
});
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

The standard way of accessing environment variables within no JS is using process N. Here we have a simple piece of code that logs out the logged in user. However, you can see that we are getting an error on the process variable. If you hover over the error, you can see that it is complaining that cannot find the name process. This is because process is not defined anywhere within our code base and we expect it to be provided by the node JS runtime. TypeScript provides the ability to declare any variables that are not present within the TypeScript code base.

00:34

For example, for the process variable, we can simply declare it as a variable of type any. The syntax for a type declaration is the same as the syntax for declaring any other variable within your code base with two key differences. First, we use declare keyword before declaring the variable, and then we cannot provide any implementation details as they are not really a declaration, but rather a definition. For example, if you try to provide a value for the process variable, TypeScript will complaint that you cannot provide any implementation

01:06

within a declaration. TypeScript supports creating these declarations within a separate file as well. These files are called declaration files. A declaration file is simply a set of declarations and has the file extension D ts. So we can take the type definition for the process variable and create a new file called ND Ts, and you are free to name your declaration files, whatever you want, as long as they end in D Ts. And then we paste this declaration for the process variable.

01:39

Now, in addition to this process variable, there are a lot of other node JS runtime features as well, and we could keep going down this path of creating these declarations ourselves, but fortunately there is a better way. So let's go ahead and delete this declaration file and compile the code using the TypeScript compiler. Now you can see on the compiler output that not only does it tell you that it cannot find this process variable, but also provides a suggestion that you might want to install the type definitions using NPMI types node. Now types is the name of an NPM organization owned

02:13

by Microsoft, and packages under this organization are automatically deployed from a very popular community-driven open source project called Definitely Typed. One of the many type definitions is the one for Node, and we can install it by running NPMI ad types slash node. Once the installation is complete, you can see that the error for the process variable goes away, and the same lack of errors can be observed if you compile a code using the TypeScript compiler. Now, if you go to the definition of the N variable within a code, you can see that it comes from a file called Process dts,

02:48

which is a part of the package that we installed, which is add types slash node. Now, the node JS runtime also provides a number of built-in modules as well, and one of them Is for file system known as fs. Now, in order to use FS or any of the other modules, we simply import it within our code and use it as you normally would within JavaScript. Now, one great thing about these type definitions worth mentioning is that in addition to providing compile time type safety, they also provide great docs for all of the APIs that you are using.

03:20

For example, for this right file sync function, that's it for the node Gs built-ins. Let's talk about third party packages that you might find on NBM. Now, many of those packages are written in TypeScript, so you don't need to do anything special other than simply installing them in order to use them in your code base. There are, however, some packages that require an external type definition, and one such example is express. If we simply install express and try to use it in a simple JavaScript application where we create an express application, set up a route

03:53

and stop listening, you can see that we get a few errors. The key one here is that the express module is not found because it is not described anywhere in TypeScript, similar to what we did for Node, we can actually simply install the type definitions for express that are maintained by the community using NPMI types slash express. Now, once the installation is complete, you can see that the errors within our IDE go away and if we run the compiler on a code base, we don't get any errors on the terminal.

Professional TypeScript Masterclass

Professional TypeScript Masterclass

1.Introduction
free
⏱️ 1:54
2.Setup
free
⏱️ 5:44
3.Primitive Types
free
⏱️ 1:42
4.Instance Types
free
⏱️ 1:52
5.Arrays And Tuples
free
⏱️ 1:38
6.Objects
free
⏱️ 1:33
7.const declarations
free
⏱️ 1:03
8.Function Types
free
⏱️ 1:57
9.Structural Typing
free
⏱️ 2:10
10.Classes in TypeScript
free
⏱️ 1:48
11.Target Compiler Option
free
⏱️ 2:37
12.Generics
⏱️ 3:02
13.Special Types any And unknown
⏱️ 2:00
14.JavaScript to TypeScript
⏱️ 1:32
15.Frontend Projects
⏱️ 3:49
16.Type Assertions
⏱️ 2:15
17.Type Casting
⏱️ 1:16
18.Modules
⏱️ 1:55
19.Type Declarations
⏱️ 4:25
20.Creating NPM packages
⏱️ 3:20
21.Async Await
⏱️ 3:05
22.Running in NodeJS
⏱️ 1:40
23.Lexical this
⏱️ 2:34
24.readonly Modifier
⏱️ 1:59
25.Union Types
⏱️ 2:57
26.Literal Types
⏱️ 2:58
27.Type Narrowing
⏱️ 4:19
28.Discriminated Unions
⏱️ 3:29
29.Class Parameter Properties
⏱️ 1:02
30.Strict Compiler Option
⏱️ 6:18
31.null vs undefined
⏱️ 4:19
32.Intersection Types
⏱️ 2:03
33.Optional Modifier
⏱️ 2:47
34.Non Null Assertion Operator
⏱️ 3:40
35.Interfaces
⏱️ 2:28
36.Interface Declaration Merging
⏱️ 1:01
37.Types vs Interfaces
⏱️ 2:16
38.never Type
⏱️ 3:00
39.implements Keyword
⏱️ 1:25
40.Definite Assignment Assertion
⏱️ 2:31
41.User Defined Type Guards
⏱️ 2:02
42.Assertion Functions
⏱️ 3:42
43.Function Overloading
⏱️ 4:15
44.Call Signatures
⏱️ 2:53
45.Abstract Classes
⏱️ 1:53
46.Index Signatures
⏱️ 3:08
47.Readonly Arrays and Tuples
⏱️ 2:58
48.Double Assertions
⏱️ 2:20
49.const Assertions
⏱️ 3:55
50.this Parameter
⏱️ 2:33
51.Generic Constraints
⏱️ 2:43
52.typeof Type Operator
⏱️ 2:12
53.Lookup Types
⏱️ 3:12
54.keyof Type Operator
⏱️ 3:55
55.Conditional Types
⏱️ 4:39
56.Contitional Types with Unions and never
⏱️ 3:32
57.infer Keyword and `ReturnType<T>`
⏱️ 3:47
58.Mapped Types
⏱️ 2:48
59.Mapped Type Modifiers
⏱️ 3:37
60.Template Literal Type
⏱️ 4:28
61.Partial<T>
⏱️ 1:27
62.Required<T>
⏱️ 1:36
63.Readonly<T>
⏱️ 1:34
64.Record<K, T>
⏱️ 4:05
65.Project References
⏱️ 4:18
66.undefined vs. optional
⏱️ 2:48
67.satisfies Operator
⏱️ 2:42
68.PropertyKey Type
⏱️ 0:57
69.ThisType<T>
⏱️ 4:11
70.Awaited<T>
⏱️ 4:12
71.String Manipulation Types
⏱️ 3:36
72.Mapped Types as Clauses
⏱️ 4:01
73.Union vs Intersection Mental Model
⏱️ 3:36
74.Enums are Bad
⏱️ 8:11