TS1361

error TS1361: ‘Range‘ cannot be used as a value because it was imported using ‘import type’.

Broken Code ❌

1
2
3
4
5
6
7
8
9
import type { Range, TextEditor } from "vscode";

export class Ranger {
static getFullRange(editor: TextEditor): Range {
const lastLine = textEditor.document.lineAt(textEditor.document.lineCount - 1);
const firstLine = textEditor.document.lineAt(0);
return new Range(firstLine.range.start, lastLine.range.end);
}
}

Fixed Code ✔️

The type Range is being used to declare the return type of static function Ranger.getFullRange but later on it is also being used to create an instance using new Range. When you want to use a type to construct instances (or do anything else beyond declaring types), then you have to use the import statement instead of import type:

1
2
3
4
5
6
7
8
9
10
import type { TextEditor } from 'vscode';
import { Range } from 'vscode';

export class Ranger {
static getFullRange(editor: TextEditor): Range {
const lastLine = textEditor.document.lineAt(textEditor.document.lineCount - 1);
const firstLine = textEditor.document.lineAt(0);
return new Range(firstLine.range.start, lastLine.range.end);
}
}