question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot define a union type with a new signature type

See original GitHub issue

This currently gives me parse error:

class A {

}

function test(x: string | new() => A) {
                          ~~~~~~~~~~ parse error    
}

Though this works:

function test(x: new() => A) {

}

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
DanielRosenwassercommented, Jul 29, 2015

I’d say that it’s advantageous to users to disallow this, since we force writers to be explicit for future readers.

But yes, I think parsing this and reporting an error would be helpful, such as:

A function or constructor type literal must be explicitly parenthesized in a union or intersection type.

or the following two separately:

A function type literal must be explicitly parenthesized in a union or intersection type.
A constructor type literal must be explicitly parenthesized in a union or intersection type.
0reactions
jbondccommented, Sep 20, 2015

Hit this recently with intersection types. Patch (was a bit tricky) uses this message : Invalid type. To avoid ambiguity, add parentheses: '(new() => void)'

Read more comments on GitHub >

github_iconTop Results From Across the Web

Union type with incompatible signatures - Stack Overflow
I'm trying to add TypeScript support and it is proving challenging. The latest problem is that I'm getting this error: TS2349: This expression ......
Read more >
Quick fix for 'unions can't be used in index signatures ... - GitHub
Gives this error message: An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
Read more >
Handbook - Unions and Intersection Types - TypeScript
A common technique for working with unions is to have a single field which uses literal types which you can use to let...
Read more >
Type Aliases vs Interfaces in TypeScript - DEV Community ‍ ‍
Type aliases and interfaces can be combined into one type using unions or intersections, but cannot be combined into an interface .
Read more >
An index signature parameter type cannot be a literal type or a ...
The error An index signature parameter type cannot be a literal type or a generic type occurs when we use an incompatible type...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found