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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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
Copy link

codecov bot commented Nov 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (0042a21) to head (5108a4f).

✅ All tests successful. No failed tests found.

Additional details and impacted files

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@matthieu-crouzet
Copy link
Contributor

matthieu-crouzet commented Nov 15, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment