Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type safety by applying satisfies operator #1972

Closed
MajorLift opened this issue Nov 1, 2023 · 4 comments
Closed

Improve type safety by applying satisfies operator #1972

MajorLift opened this issue Nov 1, 2023 · 4 comments

Comments

@MajorLift
Copy link
Contributor

MajorLift commented Nov 1, 2023

The next version bump for TypeScript in core and metamask-module-template will mark an update to version 4.9, which will make the satisfies operator keyword available for use (https://devblogs.microsoft.com/typescript/announcing-typescript-4-9/#satisfies).

The satisfies operator is a safer alternative to type annotations (val: T) and type assertions (val as T). We should investigate opportunities to improve type safety in the codebase by applying this feature to existing code.

@desi
Copy link

desi commented Mar 21, 2024

As we are going through other work we should be on the lookout for places to replace Type Annotations with satisfies. Typescript guidelines should give indicators for when to use satisfies

@desi
Copy link

desi commented Mar 21, 2024

There are currently 135 Type Annotations in core.

@desi
Copy link

desi commented Mar 21, 2024

@MajorLift will do a bit more investigation and we will revisit as part of next refinement.

@MajorLift
Copy link
Contributor Author

Type annotations count in core as of 3/21/2024 (test files excluded):

  • const \S+: : 136 results
  • let \S+: : 46 results

MajorLift added a commit to MetaMask/snaps that referenced this issue Jul 23, 2024
## Motivation

As part of its OKRs (Q2 2024 O3KR4, Q3 2024 O2KR4) the Wallet Framework
Team has upgraded all core packages to use TypeScript v5.0, and is
enabling other packages throughout our codebase to upgrade as well.
Unblocking the snaps monorepo is a high priority, as it has close
interdependencies with core monorepo.

This commit upgrades the TypeScript version used in the snaps monorepo
by two major versions: v4.9 and v5.0.

- https://devblogs.microsoft.com/typescript/announcing-typescript-4-9/
- https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/

## Features

### `v4.9`
- [`satisfies`
operator](https://devblogs.microsoft.com/typescript/announcing-typescript-4-9/#the-satisfies-operator)
  - See MetaMask/core#1972
- [Unlisted property narrowing with `in`
operator](https://devblogs.microsoft.com/typescript/announcing-typescript-4-9/#in-narrowing)

### `v5.0`
- [`const` Type
Parameters](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#const-type-parameters):
  - Causes `as const` inference by default.
  - 77 `as const` instances in the snaps monorepo.
```ts
type HasNames = { names: readonly string[] };
function getNamesExactly<const T extends HasNames>(arg: T): T["names"] {
//                       ^^^^^
    return arg.names;
}
// Inferred type without `const`: string[]
// Inferred type with `const`: readonly ["Alice", "Bob", "Eve"]
// Note: Didn't need to write 'as const' here
const names = getNamesExactly({ names: ["Alice", "Bob", "Eve"] });
```
- `enum` improvements:
- [All `enum`s are union
`enum`s](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-0.html#all-enums-are-union-enums)
- [`enum`
overhaul](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-0.html#enum-overhaul)
  - 30 `enum` instances in the snaps monorepo.
- Decorators

## Breaking Changes

### `v5.0`
- Full list of breaking changes and deprecations:
https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#breaking-changes-and-deprecations
- Regressions in Snaps repo
- [Forbidden Implicit Coercions in Relational
Operators](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#forbidden-implicit-coercions-in-relational-operators)
- Fixed here:
fffe4b2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants