@@ -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