Suggestion: Allow interface to extend conditional type
See original GitHub issueAn interface cannot currently extend a conditional type. See: (playground link)
type Test<T> = T extends {} ? { test: T } : {}
// Ok: type with selected condition
interface I extends Test<number> { }
// "Error: An interface may only extend a class or another interface."
interface J<T> extends Test<T> { }
// Ok: type without condition
type Test2<T> = { test: T }
interface K<T> extends Test2<T> { }
This example exhibits one of these problems:
J<T>
should be allowed to extendTest<T>
, to make it more complete and allow conditional type things in interfaces. –OR–- The error message should be made more clear, because interfaces can obviously extend types as you can see above.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Conditional types in TypeScript - Artsy Engineering
This extends keyword is the heart of a conditional type. A extends B means precisely that any value of type A can safely...
Read more >Conditionally getting the type of interface property in typescript
The conditional type you're going for seems to be a red flag for me. ... The conditional you want is <key> extends keyof...
Read more >Documentation - Conditional Types - TypeScript
Create types which act like if statements in the type system.
Read more >Advanced TypeScript: The Power and Limitations of ... - Medium
The infer keyword can go inside of a conditional type to the right of the extends keyword. Using infer is asking TypeScript to...
Read more >Code completion | IntelliJ IDEA Documentation - JetBrains
Expand a string at caret to an existing word · Type the initial string and do one of the following: Press Alt+/ or...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@RyanCavanaugh you mentioned the only thing we can do is update the error message; @mhegazy do you agree with that? if so, what does the diagnostics message need to say so that it is accurate than the current diagnostics message?
Hmm. If all the possible types the condition could evaluate to are object types, and an interface extends it, the interface is still an object type even if it’s conditional, no? I.e. even though it’s conditional, it’s still guaranteed to resolve to some object type.
If that’s not a strong enough guarantee because of compiler/typesystem/implementation reasons, I won’t argue it any farther. Thank you for your explanations!