Nullish Coalescing
The nullish coalescing operator is an alternative to ||
which returns the right-side expression if the left-side
is null or undefined.
In contrast, || uses falsy checks, meaning an empty
string or the number 0 would be considered false.
A good example for this feature is dealing with partial
objects which have defaults when a key isn't passed in.
// You can read more about nullish coalescing in the 3.7 blog post:
https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/
interface AppConfiguration {
// Default: "(no name)"; empty string IS valid
name: string;
// Default: -1; 0 is valid
items: number;
// Default: true
active: boolean;
}
function updateApp(config: Partial