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

function type in union type not work? #39546

Closed
keelii opened this issue Jul 10, 2020 · 6 comments
Closed

function type in union type not work? #39546

keelii opened this issue Jul 10, 2020 · 6 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@keelii
Copy link

keelii commented Jul 10, 2020

type fn = (param: number) => string

// works
type t1 = string | fn

// not works, why?
type t2 = string | (param: number) => string
@DanielRosenwasser DanielRosenwasser added the Question An issue which isn't directly actionable in code label Jul 10, 2020
@keelii
Copy link
Author

keelii commented Jul 10, 2020

But this is OK(with brackets()):

type fn = (param: number) => string

// works
type t1 = string | fn

// It's OK
type t2 = string | ((param: number) => string)

This maybe confused, the same code express different meanings.

type t2_maybe_1 = string
    | (param: number) => string
    | number

type t2_maybe_2 = string
    | (param: number) => string | number

@DanielRosenwasser
Copy link
Member

Concerns over visual ambiguity.

type Foo = () => string | () => number;

could be parsed as

type Foo = () => (string | () => number);

which isn't always what people want.

It doesn't protect against all the cases, but it's what we felt we could cover.

The fact that you don't get a good error message - now that's a terrible experience...

@DanielRosenwasser
Copy link
Member

Check out #39548 to track making the error UX better here.

@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

@DanielRosenwasser
Copy link
Member

You should now get a nicer error message thanks to #39570 by @uhyo.

@keelii
Copy link
Author

keelii commented Jul 16, 2020

@DanielRosenwasser Nice work, thx~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants