I just did some refactoring on a medium size code base and here are a few things to watch out for when adopting optional chaining and the new null coalescing operator:<p><pre><code> foo && await foo();
</code></pre>
is not the same as<p><pre><code> await foo?.();
</code></pre>
this will work in most cases but subtly, the await wraps the undefined case into a Promise, while the original code would skip the await altogether.<p>String regular expression matching returns null, not undefined, so rewriting code such as:<p><pre><code> const match = str.match(/reg(ex)/);
return match && match[1];
</code></pre>
is not the same thing as:<p><pre><code> return match?.[1];
</code></pre>
because the latter returns undefined, not null, in case of match failure. This can cause problems if subsequent code expects null for match failure. An equivalent rewrite would be:<p><pre><code> return match?.[1] ?? null;
</code></pre>
which is longer than the original and arguably less clear.<p>A common idiom to catch and ignore exceptions can interact poorly with optional chaining:<p><pre><code> const v = await foo().catch(_ => {});
return v?.field; // property 'field' does not exist on type 'void'
</code></pre>
This can be easily remedied by changing the first line to:<p><pre><code> const v = await foo().catch(_ => undefined);
</code></pre>
Of course, these new operators are very welcome and will greatly simplify and help increase the safety of much existing code. But as in all things syntax, being judicious about usage of these operators is important to maximize clarity.
I'm a big fan of the new operators.<p>One of my remaining gripes with Javascript/Typescript is the try/catch mess around await. It makes assignment of const a pain for async calls that may reject.<p>e.g.<p><pre><code> let result: SomeType;
try {
result = await funcThatReturnSomeType();
} catch (err) {
doSomethingWithErr(err);
}
// at this point result is `SomeType | undefined`
if (result) {
doSomething(result);
}
</code></pre>
I really want some kind of structures that allow me to make `result` constant. In some cases I've rolled my own Maybe/Either wrapper and then move the try/await/catch into a function but that is still a pain.<p>This is such a common pattern in my code ... I wish there was a more elegant way to deal with it.
I really like the optional chaining operator for statically typed languages. Especially in TypeScript where you have the nullabilaty information baked into the type system.<p>However, in JS itself, it might cause developers to lose track of what can be null/undefined in their code. In case they start to "fix" stuff by throwing in some "?." because they don't know better, the code maintainability will degrade a lot.<p>Maybe I'm just pessimistic. Let's see how it will perform in the field!
Optional Chaining also coming soon to create-react-app, planned for v3.3 (current release is react-scripts@3.2.0) - <a href="https://github.com/facebook/create-react-app/pull/7438" rel="nofollow">https://github.com/facebook/create-react-app/pull/7438</a><p>I think they're just waiting on support for the new syntax in prettier.
I really like the new optional operator, this might be what gets me to bite the bullet and start moving some of my projects over to typescript - dealing with potential undefined objects in those chains is one of the things I actively dislike about writing in vanilla Javascript.
now if they can just add a compiler flag for "immutable by default":<p><a href="https://github.com/microsoft/TypeScript/issues/32758" rel="nofollow">https://github.com/microsoft/TypeScript/issues/32758</a>
I feel really excited about 3.7. Optional chaining and null coalescing will clean up a TON of code.<p>... but with that being said, 3.7 seems to have broken many aspects of the `Promise.all` interface. Right now the largest issue seems to be that if any `Promise` result in `Promise.all` is nullable, all of the results are nullable.
The preview release announcement for this version made FP on HN already, but it's a biggie:<p>* Optional Chaining & Coalescing<p>* Assertion Functions<p>* .d.ts Emit From .js Files<p>* Smarter Control Flow Analysis<p>* Flatter Error Messages<p>also great:<p>* Function Truthy Checks / Uncalled Function Checks (which was tslint's biggest value prop for me up until now)
What's the end state for TypeScript? Does it one day become feature complete and slow down? One of the frustrating things about trying to find help with TS today as a beginner is the plethora of SO and blog posts talking about much older versions. If you're lucky there'll be some comment "As of 2.6 you can now do ...", but even 2.6 is a lot of versions back - how do I know that's still the best way to do it in 3.7?<p>I'm for progress, but it makes me a little wary to start my team of no-previous-TS-experience JS devs on a TypeScript project when there's still a new version every few months. Keeping up with Webpack is enough of a hassle...
Great incremental release.<p>Typescript really isn't the most exciting language, but it's very very helpful. Compared to regular javascript it saves me
a lot of time, and so many pains and headaches every day.
Does the function argument (i.e. the template string) get evaluated regardless of the optional chaining or does it match up with the “roughly equivalent” code?<p><pre><code> log?.(`Request started at ${new Date().toISOString()}`);
// roughly equivalent to
// if (log != null) {
// log(`Request started at ${new Date().toISOString()}`);
// }</code></pre>
I'm super excited about this release, but it got off to a rocky start for me. Prior to 3.7, all our tests worked fine, but something in 3.7 changed that caused the type-checker to fail previously valid code. What's worse is that I can't reproduce the issue in a playground. Thankfully the workaround is "make your code more explicit", which is fine, but it was just a surprise to see something like this break.<p>For those that are curious, here's the error that 3.7 introduced:<p><pre><code> Type 'SinonStub<[string, RequestBody, (RequestOptions | undefined)?], KintoRequest>' is not assignable to type 'SinonStub<any[], any>'.
Type 'any[]' is missing the following properties from type '[string, RequestBody, (RequestOptions | undefined)?]': 0, 1
</code></pre>
I think the issue here is that `[string, RequestBody, (RequestOptions | undefined)?]` is a tuple type, and `any[]` is an array type. That being said though, I'd expect that a tuple would satisfy a `any[]` type.
There's a bunch of website work that came out with this release too: mainly search and a lot of playground improvements.<p><a href="https://www.typescriptlang.org" rel="nofollow">https://www.typescriptlang.org</a>
For this code:<p><pre><code> const x = [1,2];
const y = x[666];
const z = y + 3;
</code></pre>
Is there a way for TypeScript to flag the last line as a type error?<p>TypeScript will say "y" has type "number" when "x[666]" returns undefined. Why does TypeScript not say the type of "y" is "number | undefined"?
Very cool features for writing shorter code! I also noticed a small mistake in their examples: <a href="https://github.com/microsoft/TypeScript-Handbook/issues/1135" rel="nofollow">https://github.com/microsoft/TypeScript-Handbook/issues/1135</a>
This is going to be one of my favorite releases since 2.8, which added conditional types. So many bad utility functions will be able to go away. The only thing it seems to be missing is variadic type generics.
From the snippets in the release notes:<p>```<p>function dispatch(x: string | number): SomeType {<p><pre><code> if (typeof x === "string") {
return doThingWithString(x);
}
else if (typeof x === "number") {
return doThingWithNumber(x);
}
process.exit(1);
</code></pre>
}<p>```<p>It's very embarrassing in my opinion that they haven't done anything yet against having these horrible kind of type checking; comparing against a string that has the type name? "string", "number"? it's completely ludicrous.<p>I will not take this language seriously until this is fixed. (For sure it's still better than JavaScript, but that's about it.)