最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting Argument of type 'string | undefined' is not assignable to parameter of type 'strin

programmeradmin0浏览0评论

I'm not sure if any of you ever came across the same issue but I've got a problem with ts-node and environment variables and your help would be much appreciated!

What I'm trying to do here is basically just to run index.ts with ts-node. And this index.ts simply prints out one of the environment variables.

index.ts:

import "dotenv/config";

const printEnv = (value: string) => {
  console.log(value);
};

printEnv(process.env.PASSWORD);

To make the environment variables available from index.ts, I created env.d.ts in which I defined the type of the variable.

env.d.ts:

declare global {
  namespace NodeJS {
    interface ProcessEnv {
      PASSWORD: string;
    }
  }
}
export {}

I set up my tsconfig.json, package.json and .env files like below.

tsconfig.json:

{
  "name": "ts-env",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "nodemon src/index.ts"
  },
  "devDependencies": {
    "@types/node": "^18.6.1",
    "nodemon": "^2.0.19",
    "ts-node": "^10.9.1",
    "typescript": "^4.7.4"
  },
  "dependencies": {
    "dotenv": "^16.0.1"
  }
}

package.json:

{
  "name": "ts-env",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "nodemon src/index.ts"
  },
  "devDependencies": {
    "@types/node": "^18.6.1",
    "nodemon": "^2.0.19",
    "ts-node": "^10.9.1",
    "typescript": "^4.7.4"
  },
  "dependencies": {
    "dotenv": "^16.0.1"
  }
}

.env:

PASSWORD=test0123

With this setup, Visual Studio Code is now able to show me the possible options whenever I type process.env.

But it fails to compile whenever I try to run this application with ts-node, saying that process.env.PASSWORD could be undefined.

Here is the error log from ts-node:

TSError: ⨯ Unable to compile TypeScript:
src/index.ts:9:10 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

9 printEnv(process.env.PASSWORD);

I did fair amount of digging but unable to find a right solution for this issue. It would be much appreciated if you could point me in the right direction.

I'm not sure if any of you ever came across the same issue but I've got a problem with ts-node and environment variables and your help would be much appreciated!

What I'm trying to do here is basically just to run index.ts with ts-node. And this index.ts simply prints out one of the environment variables.

index.ts:

import "dotenv/config";

const printEnv = (value: string) => {
  console.log(value);
};

printEnv(process.env.PASSWORD);

To make the environment variables available from index.ts, I created env.d.ts in which I defined the type of the variable.

env.d.ts:

declare global {
  namespace NodeJS {
    interface ProcessEnv {
      PASSWORD: string;
    }
  }
}
export {}

I set up my tsconfig.json, package.json and .env files like below.

tsconfig.json:

{
  "name": "ts-env",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "nodemon src/index.ts"
  },
  "devDependencies": {
    "@types/node": "^18.6.1",
    "nodemon": "^2.0.19",
    "ts-node": "^10.9.1",
    "typescript": "^4.7.4"
  },
  "dependencies": {
    "dotenv": "^16.0.1"
  }
}

package.json:

{
  "name": "ts-env",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "nodemon src/index.ts"
  },
  "devDependencies": {
    "@types/node": "^18.6.1",
    "nodemon": "^2.0.19",
    "ts-node": "^10.9.1",
    "typescript": "^4.7.4"
  },
  "dependencies": {
    "dotenv": "^16.0.1"
  }
}

.env:

PASSWORD=test0123

With this setup, Visual Studio Code is now able to show me the possible options whenever I type process.env.

But it fails to compile whenever I try to run this application with ts-node, saying that process.env.PASSWORD could be undefined.

Here is the error log from ts-node:

TSError: ⨯ Unable to compile TypeScript:
src/index.ts:9:10 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

9 printEnv(process.env.PASSWORD);

I did fair amount of digging but unable to find a right solution for this issue. It would be much appreciated if you could point me in the right direction.

Share Improve this question edited Aug 2, 2022 at 5:26 Youssouf Oumar 46k16 gold badges100 silver badges104 bronze badges asked Aug 1, 2022 at 9:03 Seuta KSeuta K 1071 gold badge1 silver badge5 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 15

The type for an environment variable is string | undefined, which makes sense because it can miss, either the entire .env file, or just that the variable is not in it.

The fix here is either to change your function definition to:

const printEnv = (value: string | undefined) => {
  console.log(value);
};

printEnv(process.env.PASSWORD);

Or tell TypeScript that the variable PASSWORD exist for sure with the ! mark, like so:

const printEnv = (value: string) => {
  console.log(value);
};

printEnv(process.env.PASSWORD!);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论