TypeScript 4.3 Beta
devblogs.microsoft.com
devblogs.microsoft.com
type RSTRIP<STR> = STR extends `${infer T} ` ? RSTRIP<T> : STR;
type LSTRIP<STR> = STR extends ` ${infer T}` ? LSTRIP<T> : STR;
type STRIP<STR> = RSTRIP<LSTRIP<STR>>;
which allows things like: function strip<T extends string>(str: T): STRIP<T> {
return str.trim() as STRIP<T>;
}
function printStripped<T>(str: STRIP<T>) {
console.log(str);
}
printStripped('hi');
//X printStripped(' hi');
printStripped(strip(' hi'));1. Something that can never be null
2. Based on code that’s not present
3. Which even if it were present wouldn’t necessarily be subject to strictNullChecks (even the example they used wouldn’t!)
Having had some more time to think about it, not only do I think it’s odd, I think it should change (and my next stop is their GH issues to say so/upvote whoever already has). It violates the principle of least surprise, which is very much at odds with the language’s mission.
Note that I’m not necessarily advocating for yet another flag/compilerOptions entry. I’d much rather they roll everything up in `strict`, and I’d even more rather they remove the non-strict option altogether for .ts files.