Home > Enterprise >  How to activate TypeScript verification of types?
How to activate TypeScript verification of types?

Time:07-01

I'm trying to follow a tutorial in which this sample of code is expected to fail. But in my machine it works just fine.

// example.ts

let age: number;

age = 15;
console.log(age);

age = "foo";
console.log(age);

This is what I see:

$ deno run example.ts
15
foo

I expected it to fail with a TS2322 code instead.

$ deno --version
deno 1.23.1 (release, x86_64-unknown-linux-gnu)
v8 10.4.132.8
typescript 4.7.2

Any insights?

CodePudding user response:

Starting in v1.23, by default, Deno does not type-check your code when executing it.

You can type-check your code (without executing it) using the command deno check. You can also type-check your code before execution by using the --check argument to deno run. Here are some examples using your example.ts module:

% deno --version
deno 1.23.1 (release, x86_64-apple-darwin)
v8 10.4.132.8
typescript 4.7.2

% cat example.ts 
let age: number;

age = 15;
console.log(age);

age = "foo";
console.log(age);

% deno check example.ts 
Check file:///Users/deno/example.ts
error: TS2322 [ERROR]: Type 'string' is not assignable to type 'number'.
age = "foo";
~~~
    at file:///Users/deno/example.ts:6:1

% deno run --check example.ts
Check file:///Users/deno/example.ts
error: TS2322 [ERROR]: Type 'string' is not assignable to type 'number'.
age = "foo";
~~~
    at file:///Users/deno/example.ts:6:1

% deno run --check=all example.ts
Check file:///Users/deno/example.ts
error: TS2322 [ERROR]: Type 'string' is not assignable to type 'number'.
age = "foo";
~~~
    at file:///Users/deno/example.ts:6:1
  • Related