JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

Fixing the “Cannot Use Namespace as a Type” Error in TypeScript

Tari Ibaba
JavaScript in Plain English
2 min readJun 4, 2022

--

Are you experiencing the “cannot use namespace as a type” error in TypeScript?

This error can occur when you try to import types declared as a module. For example:

car.d.ts

declare module 'car' {
class Car {
color: string;
age: number;
maxSpeed: number;
}
}

index.ts

import Car from 'car';// Cannot use namespace 'Car' as a type.
const user: Car = {
color: 'red',
age: 2,
maxSpeed: 120,
};

To fix this error, use an export assignment to specify a default export for the namespace, like this:

car.d.ts

declare module 'car' {
class Car {
color: string;
age: number;
maxSpeed: number;
}
export = Car;
}

Updated at: codingbeautydev.com

Get new web development tips and tutorials every week.

Subscribe

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

--

--

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Written by Tari Ibaba

Thinker + Creator. Sharing news, thoughts, and info on the latest in tech, AI, and computing.

No responses yet

Write a response