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

Bug: Intellisense shows misleading error when parameter is expected to be a union of an object and a string #45217

Open
devanshj opened this issue Jul 28, 2021 · 4 comments
Assignees
Labels
Domain: Error Messages The issue relates to error messaging Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Rescheduled This issue was previously scheduled to an earlier milestone Suggestion An idea for TypeScript

Comments

@devanshj
Copy link

devanshj commented Jul 28, 2021

Bug Report

πŸ”Ž Search Terms

Intellisense, incorrect function parameter

πŸ•— Version & Regression Information

Tried with v4.3.5

⏯ Playground Link

Playground

πŸ’» Code

declare const send:
  (event: { type: "A", foo: string } | { type: "B" } | "B") => void

declare const sendOnlyObject:
  (event: { type: "A", foo: string } | { type: "B" }) => void


send({ type: "A" })
// Type '"A"' is not assignable to type '"B"'.(2322)

sendOnlyObject({ type: "A" })
// Argument of type '{ type: "A"; }' is not assignable to parameter of type '{ type: "A"; foo: string; } | { type: "B"; }'.
//   Property 'foo' is missing in type '{ type: "A"; }' but required in type '{ type: "A"; foo: string; }'.(2345)

πŸ™ Actual behavior

The error for send({ type: "A" }) is Type '"A"' is not assignable to type '"B"'

πŸ™‚ Expected behavior

The error should be something like the one for sendOnlyObject or at least not misleading that the property type has to be "B" that's what is getting suggested here.

@andrewbranch andrewbranch added Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript labels Aug 5, 2021
@andrewbranch andrewbranch added this to the TypeScript 4.5.0 milestone Aug 5, 2021
@DanielRosenwasser DanielRosenwasser added Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this labels Aug 5, 2021
@DanielRosenwasser
Copy link
Member

It's probably the case that we bail early in findMatchingDiscriminantType because of the primitive. If it's not too hard and it's not too expensive, the fix is likely to create a union of only object types, match on the discriminants of the union, and elaborate on that type.

@MonstraG
Copy link

MonstraG commented Sep 10, 2021

I'm not sure if it's related, but I've just hit TS2345 on this:

const func = (foo: boolean | "bar") => {}
func(1 != 2 && "foo")
     ^^^^^^^^^^^^^^^
TS2345: Argument of type '"" | "foo"' is not assignable to parameter of type 'boolean | "bar"'.   Type '""' is not assignable to type 'boolean | "bar"

(this contrived example also gives TS2367 but we don't care about that)

It looks like for some reason, if I | with a string, first false is resolved as "" instead

@ldwonday
Copy link

ldwonday commented Dec 6, 2021

Is the problem fix in v4.5.2?

@MonstraG
Copy link

MonstraG commented Dec 6, 2021

Neither OP nor mine is fixed on 4.5.2

@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Error Messages The issue relates to error messaging Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Rescheduled This issue was previously scheduled to an earlier milestone Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants