-
-
Notifications
You must be signed in to change notification settings - Fork 505
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
Add EmptyObject
(the type you'd expect for {}
)
#395
Comments
Makes sense for discoverability 👍 |
I like this idea. However there may some issues due to unexpected behavior in niche instances. I have just one of those and an adjustment. First, this would be more complete: type EmptyObject = Record<keyof any, never>; Second, I saw this example from someone on stack overflow, which seems to violate the type safety of this. type EmptyObject = Record<keyof any, never>;
type Union = EmptyObject | { id: string };
const a: Union = {};
const b: string = a.id // no error
const c = a.id; // inferred type: `never`; |
@fregante type EmptyObject = Record<never, never>;
const foo: EmptyObject = {a: 1}; // should error, but is allowed In fact, the tooltip suggests |
import type { InvariantOf } from 'type-fest';
type EmptyObject = Record<any, InvariantOf<unknown>>;
// test cases
const foo: EmptyObject = {a: 1}; // errors
const bar: EmptyObject = {}; // does not error
const baz: string = bar.a; // errors
// for comparison, both `Record<keyof any, never>` and `Record<never, never>` fail one of these |
Coming back to this a while later, import type { InvariantOf } from 'type-fest';
type EmptyObject = Record<any, InvariantOf<unknown>>;
const foo: EmptyObject = {};
const bar = foo.a; // doesn't error, but should declare const tag: unique symbol;
type EmptyObject = { [tag]?: never };
const foo: EmptyObject = {};
const bar = foo.a; // errors as expected Playground link demonstrating the difference between these two suggestions. The errors generated by the latter are also easier to understand. |
@bbrk24 Interested in submitting a pull request for it? No worries if not. |
I don't want to right now, if only because I'm not sure how much to put in the doc comment. |
https://stackoverflow.com/a/60112239/288906
Upvote & Fund
The text was updated successfully, but these errors were encountered: