Goal: Do not get into the habit of ignoring ts compiler warnings. We don't always know better.
Currently we have a helper property that checks whether we have workspace folders:
- Checks whether
vscode.workspace.workspaceFolders is an array
- Checks whether length of array
vscode.workspace.workspaceFolders is > 0
The use of this property leads to a poor programming practice:
Lets look at the following code:
if (this.workspaceService.hasWorkspaceFolder){
return
}
const workspaceFolder = this.workspaceService.workspaceFolders[0];
// Do something with the above item
- Based on the property
hasWorkspaceFolders, we (developers of exension) know that we will have at least one folder.
- However typescript doesn't know this, it throws a compiler warning about the fact that
workspaceFolders could be undefined or could have a length of 0
- To get around this, we basically tell typescript that we know better and ignore the warning as follows:
const workspaceFolder = this.workspaceService.workspaceFolders![0]!;
- Note the use of the
!.
- This is us telling typescript that we know better and it is known not to be null.
Unfortunately, i've found that we've been using this even in cases when we shouldn't. I.e. we think we know better, and we're ignoring the warning, when we shoudln't.
Solution: Delete the hasWorkspaceFolders property
Goal: Do not get into the habit of ignoring ts compiler warnings. We don't always know better.
if (this.workspaceService.hasWorkspaceFolder){
return
}
// Variations of the following code.
if ((this.workspaceService.workspaceFolders || []).length === 0){
return
}
@microsoft/pvsc-team
Goal: Do not get into the habit of ignoring
ts compilerwarnings. We don't always know better.Currently we have a helper property that checks whether we have workspace folders:
vscode.workspace.workspaceFoldersis an arrayvscode.workspace.workspaceFoldersis > 0The use of this property leads to a poor programming practice:
Lets look at the following code:
hasWorkspaceFolders, we (developers of exension) know that we will have at least one folder.workspaceFolderscould be undefined or could have a length of0!.Unfortunately, i've found that we've been using this even in cases when we shouldn't. I.e. we think we know better, and we're ignoring the warning, when we shoudln't.
Solution: Delete the
hasWorkspaceFolderspropertyGoal: Do not get into the habit of ignoring
ts compilerwarnings. We don't always know better.@microsoft/pvsc-team