Unable to specify return type of class extends T implements IFooable
See original GitHub issueTypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)
Code
// A *self-contained* demonstration of the problem follows...
export type Constructor<T> = new (...args: any[]) => T;
export interface IFooable { FooProp: string; }
export function FooableMixin<T extends Constructor<{}>>(Base: T) {
return class extends Base implements IFooable {
FooProp: string;
constructor(...args: any[]) {
super(...args);
}
}
}
export class BaseBar { BarProp: string = "baz"; }
export class FooableBar extends FooableMixin ( BaseBar ) {}
let foobar = new FooableBar();
foobar.FooProp = foobar.BarProp;
/* This is ok since FooableBar extends an Anonymous class inheriting from BaseBar and implementing IFooable */
If I turn on “declaration: true” in tsconfig.json this constuct breaks since the Anonymous class counts as private and not exported type (error TS4060) which is kind of natural since it is Anonymous.
What we need is a way to describe Classes as return types.
Expected behavior:
A d.ts file with a type declaration for the Mixin function Fooable that defines a Anonymous Class return type in the style of:
declare function Fooable(T extends Constructor<{}>): (class extends T implements IFooable);
Actual behavior:
TS4060 error when compiling with the “declaration” flag. If casting the Anonymous Class return value to <any> or T, the interface is lost.
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
java - Why is "extends T" allowed but not "implements T"?
"extends" is used to define sub-interfaces as well as sub-classes. ... You restrict a type parameter by another type and it doesn't matter ......
Read more >A tour of the Dart language
This page shows you how to use each major Dart feature, from variables and operators to classes and libraries, with the assumption that...
Read more >Returning a Value from a Method (The Java™ Tutorials ...
The data type of the return value must match the method's declared return type; you can't return an integer value from a method...
Read more >The Basics of Java Generics
It doesn't know what data type is returned. ... type T extends the upper bound in case of a class or implements an...
Read more >Really Advanced Typescript Types
The first property is the reason compilation fails in the above example; you're trying to assign an input type T to never because...
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
Nice!
Looks like a recent release (3.6.4?) has fixes that allow my lib to specify
"types": "./src/index.ts"
in package.json (pointing directly to a regular TS file, not a.d.ts
declaration file!) without any issues.This allows me to set
"declaration": false
in tsconfig.json, and rely on the actual code for type checking, which allows the return types to be inferred!It does mean that I need to ship the source TS files instead of just declaration files.
I too have problems with declarations and mixins. Shipping the source is an option but I would prefer otherwise. Any idea on how to do that?