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.

Unable to specify return type of class extends T implements IFooable

See original GitHub issue

TypeScript 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:open
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
trusktrcommented, Oct 14, 2019

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.

0reactions
nuthinkingcommented, Feb 17, 2021

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?

Read more comments on GitHub >

github_iconTop 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 >

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