Skip to content

Commit 372414d

Browse files
bors[bot]Veetaha
andauthored
Merge #3942
3942: vscode: fix typing bug in config r=matklad a=Veetaha I noticed that the type of nullable properties in config is actually non-nullable ![Screenshot from 2020-04-11 15-29-45](https://user-images.githubusercontent.com/36276403/79043702-6a686d80-7c09-11ea-9ae8-f1a777c7d0f2.png) Co-authored-by: veetaha <[email protected]>
2 parents 0679d8c + 12e23bd commit 372414d

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

editors/code/src/config.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,44 @@ export class Config {
6666
return vscode.workspace.getConfiguration(this.rootSection);
6767
}
6868

69-
get serverPath() { return this.cfg.get<null | string>("serverPath")!; }
70-
get channel() { return this.cfg.get<UpdatesChannel>("updates.channel")!; }
71-
get askBeforeDownload() { return this.cfg.get<boolean>("updates.askBeforeDownload")!; }
72-
get traceExtension() { return this.cfg.get<boolean>("trace.extension")!; }
69+
/**
70+
* Beware that postfix `!` operator erases both `null` and `undefined`.
71+
* This is why the following doesn't work as expected:
72+
*
73+
* ```ts
74+
* const nullableNum = vscode
75+
* .workspace
76+
* .getConfiguration
77+
* .getConfiguration("rust-analyer")
78+
* .get<number | null>(path)!;
79+
*
80+
* // What happens is that type of `nullableNum` is `number` but not `null | number`:
81+
* const fullFledgedNum: number = nullableNum;
82+
* ```
83+
* So this getter handles this quirk by not requiring the caller to use postfix `!`
84+
*/
85+
private get<T>(path: string): T {
86+
return this.cfg.get<T>(path)!;
87+
}
88+
89+
get serverPath() { return this.get<null | string>("serverPath"); }
90+
get channel() { return this.get<UpdatesChannel>("updates.channel"); }
91+
get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); }
92+
get traceExtension() { return this.get<boolean>("trace.extension"); }
93+
7394

7495
get inlayHints() {
7596
return {
76-
typeHints: this.cfg.get<boolean>("inlayHints.typeHints")!,
77-
parameterHints: this.cfg.get<boolean>("inlayHints.parameterHints")!,
78-
chainingHints: this.cfg.get<boolean>("inlayHints.chainingHints")!,
79-
maxLength: this.cfg.get<null | number>("inlayHints.maxLength")!,
97+
typeHints: this.get<boolean>("inlayHints.typeHints"),
98+
parameterHints: this.get<boolean>("inlayHints.parameterHints"),
99+
chainingHints: this.get<boolean>("inlayHints.chainingHints"),
100+
maxLength: this.get<null | number>("inlayHints.maxLength"),
80101
};
81102
}
82103

83104
get checkOnSave() {
84105
return {
85-
command: this.cfg.get<string>("checkOnSave.command")!,
106+
command: this.get<string>("checkOnSave.command"),
86107
};
87108
}
88109
}

0 commit comments

Comments
 (0)