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

fix: make typings more accurate by avoiding explict cast on consts #2475

Merged
merged 1 commit into from
Dec 2, 2024

Conversation

kpanot
Copy link
Contributor

@kpanot kpanot commented Nov 15, 2024

Proposed change

When creating an object that is intended to be read-only, we should let typescript infer the type.

Not good:

interface NamedObjectMaybe {
  name?: string;
}
const UNMOVABLE_OBJECT: NamedObjectMaybe = {
  name: 'unicorn'
};

// UNMOVABLE_OBJECT.name is typed as string | undefined

Nice:

interface NamedObjectMaybe {
  name?: string;
}
const UNMOVABLE_OBJECT = {
  name: 'unicorn'
} as const satisfies NamedObjectMaybe;

// UNMOVABLE_OBJECT.name is typed as 'unicorn' but we still validate that UNMOVABLE_OBJECT matches the interface

Related issues

@kpanot kpanot requested a review from a team as a code owner November 15, 2024 08:22
fpaul-1A
fpaul-1A previously approved these changes Nov 15, 2024
sdo-1A
sdo-1A previously approved these changes Nov 15, 2024
mrednic-1A
mrednic-1A previously approved these changes Nov 15, 2024
@matthieu-crouzet
Copy link
Contributor

matthieu-crouzet commented Nov 15, 2024

@kpanot kpanot dismissed stale reviews from mrednic-1A, sdo-1A, and fpaul-1A via 89cdb57 November 29, 2024 01:42
@kpanot kpanot force-pushed the chore/more-precise-typings branch 2 times, most recently from 89cdb57 to c374215 Compare November 29, 2024 06:56
fpaul-1A
fpaul-1A previously approved these changes Nov 29, 2024
@kpanot kpanot force-pushed the chore/more-precise-typings branch from d51da86 to 7cf3e2e Compare December 2, 2024 01:32
@kpanot kpanot force-pushed the chore/more-precise-typings branch from 7cf3e2e to 1603bff Compare December 2, 2024 02:21
@kpanot kpanot added this pull request to the merge queue Dec 2, 2024
Merged via the queue into main with commit ce9430c Dec 2, 2024
34 of 35 checks passed
@kpanot kpanot deleted the chore/more-precise-typings branch December 2, 2024 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment