我的代码是:
const port: Number = process.env.PORT || 3000;
[ts]
Type 'string | 3000' is not assignable to type 'Number'.
Type 'string' is not assignable to type 'Number'.
Run Code Online (Sandbox Code Playgroud)
我试过
const port: Number = parseInt(process.env.PORT, 10) || 3000;
Run Code Online (Sandbox Code Playgroud)
但它给了我另一个错误:
Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.
(property) NodeJS.Process.env: NodeJS.ProcessEnv
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Sha*_*oon 25
const port: Number = parseInt(<string>process.env.PORT, 10) || 3000
Run Code Online (Sandbox Code Playgroud)
这解决了它。我认为它被称为类型断言
| 归档时间: |
|
| 查看次数: |
10141 次 |
| 最近记录: |