There was a brief period where this feature existed, but it has since been removed. The rationale behind its removal is:
Sometimes when using TypeScript to build a library, that library may be used in a project that does not have strict null checks enabled. In such cases, having the ability to check for null values in your code becomes important. This is not necessarily for your own project's code, but for other projects that may use your library.
If you're interested, you can take a look at the pull request that explains the decision here:
https://github.com/microsoft/TypeScript/pull/8452
From what I could find, there are currently no TypeScript config options available to achieve the specific behavior you're looking for.
On a related note:
One might argue that checking for 111
should also be considered valid under certain circumstances, as another project calling the method could potentially pass that value. As a result, typeof
checks are generally allowed, even if they may seem out of place within your own project:
function test(a: string) {
// Ensure 'a' is a valid string
if (a == null || typeof a !== 'string') {
console.error("illegal argument");
return;
}
console.log(a);
}
test("a");
test(123 as any);
test(null as any);