TS1371

error TS1371: This import is never used as a value and must use ‘import type’ because ‘importsNotUsedAsValues’ is set to ‘error’.

Broken Code ❌

MyPerson.ts
1
2
3
4
export class MyPerson {
constructor(public name: string, public age: number) {
}
}
printAge.ts
1
2
3
4
5
import {MyPerson} from './MyPerson';

export function printAge(person: MyPerson) {
console.log(person.age);
}

Fixed Code ✔️

Solution 1:

When “importsNotUsedAsValues” is set to “error” in your “tsconfig.json”, then you have to use the “import type” syntax when you just want to refer to a type instead of using it as a value:

printAge.ts
1
2
3
4
5
import type {MyPerson} from './MyPerson';

export function printAge(person: MyPerson) {
console.log(person.age);
}

Solution 2:

Use the class also as a value and not just a type:

printAge.ts
1
2
3
4
5
6
7
8
import {MyPerson} from './MyPerson';

export function printAge(person: MyPerson) {
console.log(person.age);
}

const benny = new MyPerson('Benny', 35);
printAge(benny);

Solution 3:

You can also set “importsNotUsedAsValues” to “preserve” which is not recommended.