Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

use an as to rename the module:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading