如何激活类型的打字稿验证?

发布于 2025-02-12 08:13:33 字数 452 浏览 0 评论 0原文

我正在尝试遵循一个教程,其中预计该代码样本将失败。但是在我的机器中,它可以正常工作。

// example.ts

let age: number;

age = 15;
console.log(age);

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

这就是我看到的:

$ deno run example.ts
15
foo

我希望它会用 ts2322 代码失败。

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

有见地吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2025-02-19 08:13:33

从v 1.23开始,默认情况下,DENO执行该代码不会键入您的代码。

您可以使用命令deNo键入代码(无执行)键入代码检查。您还可以使用- 检查参数到deNo Run之前键入代码。以下是一些使用您的示例.ts模块的示例:

% 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文