A piece of TypeScript code I recently wrote goes as follows:
type FeatureFlagConfig = { enabled: false }
| {
enabled: true;
key: string;
};
Interestingly, when I saved the code in VSCode, it automatically reformatted to:
type FeatureFlagConfig =
| { enabled: false }
| {
enabled: true;
key: string;
};
What catches my attention is the use of | a | b
syntax instead of a | b
. This addition of the initial |
(vertical bar) might seem odd, but it seems to be the preferred style by linters. Is this purely for formatting purposes to have each union member on its own line? And is this format considered valid TypeScript syntax? It's not explicitly mentioned in the TypeScript specification here: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#386-union-type-literals