I am using typescript to write a vs code extension and for some reason I am unable to get the current line.
The function I am trying to make is:
function makeFrame()
{
vscode.window.activeTextEditor.selection.active.line;
}
Which fails with error: Object is possibly undefined The import statement is:
import {window, commands, Disposable, ExtensionContext, StatusBarAlignment, StatusBarItem, TextDocument} from 'vscode';
What am I doing wrong?
(I am both new to TypeScript and writing extensions for VS code)
activeTextEditor
may be undefined
. This indicates that there is no active editor and will happen for example when you first open a new workspace or when you close all editors
To fix, just add a quick check:
function makeFrame()
{
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
activeEditor.selection.active.line;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With