From 12e23bd60b9c3dde24bb9fa597ad88705abd2b76 Mon Sep 17 00:00:00 2001 From: veetaha Date: Sat, 11 Apr 2020 15:23:07 +0300 Subject: vscode: fix typing bug in config --- editors/code/src/config.ts | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 21c1c9f23..35a05131c 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -66,23 +66,44 @@ export class Config { return vscode.workspace.getConfiguration(this.rootSection); } - get serverPath() { return this.cfg.get("serverPath")!; } - get channel() { return this.cfg.get("updates.channel")!; } - get askBeforeDownload() { return this.cfg.get("updates.askBeforeDownload")!; } - get traceExtension() { return this.cfg.get("trace.extension")!; } + /** + * Beware that postfix `!` operator erases both `null` and `undefined`. + * This is why the following doesn't work as expected: + * + * ```ts + * const nullableNum = vscode + * .workspace + * .getConfiguration + * .getConfiguration("rust-analyer") + * .get(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(path: string): T { + return this.cfg.get(path)!; + } + + get serverPath() { return this.get("serverPath"); } + get channel() { return this.get("updates.channel"); } + get askBeforeDownload() { return this.get("updates.askBeforeDownload"); } + get traceExtension() { return this.get("trace.extension"); } + get inlayHints() { return { - typeHints: this.cfg.get("inlayHints.typeHints")!, - parameterHints: this.cfg.get("inlayHints.parameterHints")!, - chainingHints: this.cfg.get("inlayHints.chainingHints")!, - maxLength: this.cfg.get("inlayHints.maxLength")!, + typeHints: this.get("inlayHints.typeHints"), + parameterHints: this.get("inlayHints.parameterHints"), + chainingHints: this.get("inlayHints.chainingHints"), + maxLength: this.get("inlayHints.maxLength"), }; } get checkOnSave() { return { - command: this.cfg.get("checkOnSave.command")!, + command: this.get("checkOnSave.command"), }; } } -- cgit v1.2.3