-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Union type of an object literal type and empty object (using never
as its field) not reporting type errors in some cases
#51211
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
Another duplicate of #9998. |
Sorry I didn't see the template of https://github.com/microsoft/TypeScript/issues/new?assignees=&labels=Duplicate&template=types-not-correct-in-with-callback.md&title=. This seems to fall under the second category of "function calls do not reset narrowing"? But I'm still confused. With the example below, where type FooWithNever = { a: { b: string } } | Record<string, never>
type FooWithEmptyObj = { a: { b: string } } | {}
function fun1(foo: FooWithNever) {
foo.a.b // No error
}
function fun2(foo: FooWithEmptyObj) {
foo.a.b // Property 'a' does not exist on type '{}'.(2339)
} |
The type The type |
Also worth noting is that, despite ESLint’s objections, |
I completely skipped reading the part about ESLint. Bad defaults, confusing developers since forever. |
That explains. Thanks guys. Will close the ticket now |
Bug Report
typescript-eslint
recommends the use ofRecord<string, never>
to represent empty objects, because{}
means anything butnull
orundefined
. However, when this empty object type is used with other object literal types in a union type, TypeScript doesn't error where expected.🔎 Search Terms
never
type🕗 Version & Regression Information
never
, empty object and union.⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
In the functions, accessing the union-typed variable doesn't report errors.
🙂 Expected behavior
In the functions, accessing the union-typed variable should be reporting errors.
The text was updated successfully, but these errors were encountered: