Issue
My code is:
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'.
I tried
const port: Number = parseInt(process.env.PORT, 10) || 3000;
But it gives me another error:
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
What am I doing wrong?
Solution
const port: Number = parseInt(<string>process.env.PORT, 10) || 3000
This solved it. I think it's called Type Assertion
Answered By - Shamoon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.