Home > OS >  How can I import something as both a type and a value in TypeScript?
How can I import something as both a type and a value in TypeScript?

Time:09-12

I have some TypeScript code that has values as the PublicKey type, that also uses the PublicKey constructor to turns strings into PublicKeys.

import type { Connection, Keypair, PublicKey } from "@solana/web3.js";
import { PublicKey } from "@solana/web3.js";

This fails with the error:

Duplicate identifier 'PublicKey'

How can I use Keypair both as a type and a value?

CodePudding user response:

use an as to rename the module:

import type { Connection, Keypair, PublicKey } from "@solana/web3.js";
import { PublicKey as PublicKeyConstructor } from "@solana/web3.js";
  • Related