Skip to content

Commit

Permalink
refactor(core): type EMPTY_OBJ as never for improved type safety (a…
Browse files Browse the repository at this point in the history
…ngular#53571)

Consider a snippet like:

```
const x = directiveDef.inputs || EMPTY_OBJ
```

this currently results in `x` being inferred as just `{}`- ending up
turning of potential future assignment checks. This surfaced in the
`DirectiveDefinition` -> `DirectiveDef` conversion.

Note: This has the effect that assigning `EMPTY_OBJ` to a field of
anything would _always_ pass. It's questionable if this rather impacts
type-safety in a more negative way. There seem to be trade-offs in both
ways... Maybe worth considering just using `{}` directly as fallbacks in
some places, and treating this as an unique symbol?!

https://www.typescriptlang.org/play?#code/MYewdgzgLgBAHgLhmApgNxQJxgXhgbwF8YBDCZdLAbgCgbRJYAHJfGmDmAGxC6SkwBXFABoahAD6CwAExQAzAJaoZuZIK5dS5EmACeteuGgxBapjAkT4VIA

PR Close angular#53571
  • Loading branch information
devversion authored and amilamen committed Jan 26, 2024
1 parent 32c55b9 commit 61447da
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/util/empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {initNgDevMode} from './ng_dev_mode';
* code.
*/

export const EMPTY_OBJ: {} = {};
export const EMPTY_OBJ: never = {} as never;
export const EMPTY_ARRAY: any[] = [];

// freezing the values prevents any code from accidentally inserting new values in
Expand Down

0 comments on commit 61447da

Please sign in to comment.