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
Rule proposal: use satisfies over type annotation #6347
Comments
🤔 hmm. We generally try not to take in rules that ban swathes of TypeScript syntax. You can achieve this rule (minus auto-fixing) with {
"rules": {
"no-restricted-syntax": [
"error",
{
"message": "Prefer using the satisfies operator, not explicit type annotations.",
"selector": "VariableDeclarator > Identifier[typeAnnotation]"
}
]
}
} I think we should leave this open for a little while and see if there's a lot of demand for it. You might consider publishing a standalone ESLint plugin with a custom rule if you want auto-fixing. |
Could you expand on what you mean here? |
Yes, the object satisfying a record type is the use case I have in mind. And as you said otherwise there is no difference, that's why I would prefer enforcing |
In that case - as Josh said - you can achieve this with https://typescript-eslint.io/linting/troubleshooting#how-can-i-ban-specific-language-feature |
Just to spell out a bit more of the difference between them, |
@bmish yes that's really the intended usecase of satisfies really - it allows you to validate that a type is assignable to some other type without coercing it to that type. The motivating usecase for it was really to replace useless no-op generic functions people would use. fuction asOptions<T extends Record<string, boolean>>(arg: T): T {
return arg;
}
// bad as you lose the keys
const foo = {prop: true} as Record<string, boolean>;
// old style - Bad because useless fn call
const bar = asOptions({prop: true});
// new style - good - no runtime code for type check
const baz = {prop: true} satisfies Record<string, boolean>; To be clear - we're not saying that there aren't uses for satisfies - not at all! It's a great tool to be used in the right places. Instead all we're saying is that we don't want to include rules that are simple syntax bans unless there's additional value we can add in the rule (like auto or suggestion fixers) |
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Description
My rule would check that variables with type annotation should instead use the new
satisfies
operator.Fail Cases
Pass Cases
Additional Info
Since
satisfies
is available it seems like its usage is often more correct than type annotation. Hopefully, typescript-eslint could help enforce its usage to have better type safety. 😄The text was updated successfully, but these errors were encountered: