Skip to content

using .env with typesript #2621

Answered by EazyReal
EazyReal asked this question in Q&A
Discussion options

You must be logged in to vote

@EazyReal I think your main issue with Typescript is that process.env.WHATEVER can be undefined so Typescript complains on var being possibly undefined, and Ethers needs some string. (see https://docs.ethers.io/v5/api/signer/#Wallet-constructor) So you could type check before Wallet constructor :

if(!process.env.DEPLOYER_PRIVATE_KEY || process.env.DEPLOYER_PRIVATE_KEY.substring(0, 2) !== '0x')
  throw new Error('Need private key string !');

Thanks, I solved it with the ! operator
let deployer = new Wallet(process.env.DEPLOYER_PRIVATE_KEY!);

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by EazyReal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
documentation Documentation related issue.
3 participants
Converted from issue

This discussion was converted from issue #2476 on January 31, 2022 03:59.