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

Viewed 27

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?

1 Answers

use an as to rename the import:

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