When writing the following code in TypeScript, no errors are produced:
const maybe_a_string: undefined | string = undefined;
const false_or_string: false | string = false;
// I want to receive an error or warning for this line...
const message_string = `Some readable string info should be here: ${maybe_a_string} ${false_or_string}`;
Is there a setting that can be enabled or simpler ways to rewrite the last line to get a warning when using non-string variables inside strings like this? (without having to manually assert each sub-string)
It seems that TypeScript considers it valid because certain types like booleans, numbers, and miscellaneous objects have a .toString()
method...
However, particularly with undefined
(which actually lacks a .toString() method), it's common to introduce bugs where unintentional occurrences of "undefined" appear within strings. This often happens in situations where end users see unexpected phrases like "hello undefined."