-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Enhancement: [no-empty-object-type] Checks for generated empty-object-type #10619
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
Comments
IMO this would be a hard no because checking for this requires type information. There's potential scope for this in a new rule -- but also this seems like it would be a difficult thing to check. What does it mean to "generate" a type? |
I see. I agree that it's not appropriate as an option of Still, it would be nice if there was some other way to prevent mistakes like the one above. type Data = { name: string; num: number };
type NullableData = null | Data;
function foo(param: Omit<NullableData, 'name'>) { }
foo("aaa"); // no error
foo({}); // no error
foo(1); // no error
What if we only checked when type operations are used and when they are used as generic arguments? type Computed = Omit<NullableData, 'name'>; // check
function foo (arg: Computed) { // Uncheck for the references
} |
But how can we tell that This is the difficult problem to solve. At its core you really want to just check every single intersection type across the codebase to look for |
Okay, so as an alternative, what do you think about adding a rule like type Data = { name: string; num: number };
type NullableData = null | Data;
function wrongFunc(param: Omit<NullableData, 'name'> // Unexpected "{}"
) { }
wrongFunc("aaa");
wrongFunc(123);
wrongFunc({});
type WrongFunc< T extends Omit<NullableData, 'name'> // Unexpected "{}"
> = T;
type A = WrongFunc<1>;
type B = WrongFunc<{}>;
// argument, it is easy to detect with ts error.
function foo(arg: number) {}
declare const arg: Omit<NullableData, 'name'> ;
foo(arg); // ts error 👍 |
This makes me nervous. I don't have hard data, but I suspect If someone could make a prototype version of the rule and run it on typescript-eslint & a few other large codebases, that would be helpful. I think we'll want to see real-world proof of whether this is helpful. |
Sounds like a good idea. I'll look into it. Thanks |
Hi, @JoshuaKGoldberg I couldn't find any particular errors in trcp and strapi. typescript-eslint/packages/eslint-plugin/src/rules/no-unsafe-assignment.ts Lines 368 to 370 in 052b7f9
A meaningful error was found in the nest project. (here) // {} type
const keepaliveKeys: Record<
keyof GrpcOptions['options']['keepalive'],
string
> = { As shown in this PR, an incorrect type operation resulted in a |
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Link to the rule's documentation
https://typescript-eslint.io/rules/no-empty-object-type/
Description
I suggest that the
no-empty-object-type
rule also disallows{}
generated by type transformations.For example, using
Omit<V,K>
on a type that is nullable will unintentionally create a{}
type.Fail
Pass
Additional Info
Perhaps it would be appropriate to handle this in no-unsafe-*?
The text was updated successfully, but these errors were encountered: