vscode: fix typing bug in config

This commit is contained in:
veetaha 2020-04-11 15:23:07 +03:00
parent 0ecdba20df
commit 12e23bd60b

View File

@ -66,23 +66,44 @@ export class Config {
return vscode.workspace.getConfiguration(this.rootSection); return vscode.workspace.getConfiguration(this.rootSection);
} }
get serverPath() { return this.cfg.get<null | string>("serverPath")!; } /**
get channel() { return this.cfg.get<UpdatesChannel>("updates.channel")!; } * Beware that postfix `!` operator erases both `null` and `undefined`.
get askBeforeDownload() { return this.cfg.get<boolean>("updates.askBeforeDownload")!; } * This is why the following doesn't work as expected:
get traceExtension() { return this.cfg.get<boolean>("trace.extension")!; } *
* ```ts
* const nullableNum = vscode
* .workspace
* .getConfiguration
* .getConfiguration("rust-analyer")
* .get<number | null>(path)!;
*
* // What happens is that type of `nullableNum` is `number` but not `null | number`:
* const fullFledgedNum: number = nullableNum;
* ```
* So this getter handles this quirk by not requiring the caller to use postfix `!`
*/
private get<T>(path: string): T {
return this.cfg.get<T>(path)!;
}
get serverPath() { return this.get<null | string>("serverPath"); }
get channel() { return this.get<UpdatesChannel>("updates.channel"); }
get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); }
get traceExtension() { return this.get<boolean>("trace.extension"); }
get inlayHints() { get inlayHints() {
return { return {
typeHints: this.cfg.get<boolean>("inlayHints.typeHints")!, typeHints: this.get<boolean>("inlayHints.typeHints"),
parameterHints: this.cfg.get<boolean>("inlayHints.parameterHints")!, parameterHints: this.get<boolean>("inlayHints.parameterHints"),
chainingHints: this.cfg.get<boolean>("inlayHints.chainingHints")!, chainingHints: this.get<boolean>("inlayHints.chainingHints"),
maxLength: this.cfg.get<null | number>("inlayHints.maxLength")!, maxLength: this.get<null | number>("inlayHints.maxLength"),
}; };
} }
get checkOnSave() { get checkOnSave() {
return { return {
command: this.cfg.get<string>("checkOnSave.command")!, command: this.get<string>("checkOnSave.command"),
}; };
} }
} }