Skip to content

Faulty declarations file from extending generic base class #17805

Closed

Description

TypeScript Version: 2.4.1

Code

declare type Newable<TType> = {
    new (...params: any[]): TType;
};  

interface Bar<T>{
    (): T;
}

const FooBar = {};

class Foo<TItem> extends (FooBar as any as Newable<Bar<TItem>>) {

}

Expected behavior:
The generated declaration file should contain the following

declare type Newable<TType> = {
    new (...params: any[]): TType;
};
interface Bar<T> {
    (): T;
}
declare const FooBar: {};
declare const Foo_base: Newable<Bar<any>>;
declare class Foo<TItem> extends Foo_base {
}

Actual behavior:

declare type Newable<TType> = {
    new (...params: any[]): TType;
};
interface Bar<T> {
    (): T;
}
declare const FooBar: {};
declare const Foo_base: Newable<Bar<TItem>>;  // <----- TItem is not defined
declare class Foo<TItem> extends Foo_base {
}

When the generated declarations file is referenced by other code, the compilation fails with

error TS2304: Build:Cannot find name 'TItem'.

Activity

  1. weswigham commented on Mar 20, 2018

    @weswigham
    Member

    Mohamed Hegazy (@mhegazy) The example in the OP is (correctly) an error nowadays: Base class expressions cannot reference class type parameters, thanks to #17922. If the OP is still active and curious why, see the discussion in #17829 which is somewhat similar to this.

  2. weswigham commented on Mar 21, 2018

    @weswigham
    Member

    @NorlAbrahams referencing class type parameters in the extends clause doesn't make sense - the extends clause is executed once (to find the base class), but the type parameters are reinstantiated on every constructor call. If the goal is to pass on the generic into the extending class, that's still possible (ie, extending a generic base class), it's just not possible to use the type parameters in the actual extends clause expression, since they'll never be instantiated with real types there.

  3. NoelAbrahams commented on Mar 21, 2018

    @NoelAbrahams
    Author

    Wesley Wigham (@weswigham) I had already chickened out and deleted my comment. But thanks for the explanation!

  4. mrazorvin commented on Mar 30, 2018

    @mrazorvin

    Hello,

    I don't understand why did you do that ? This was only single way to create generic mixins. Now I can't migrate to 2.8 because there is no alternative for this feature

    This code works perfect in 2.3.2

    // The goal to add generic method via mixin 
    
    function ValidatorMixin<T extends { new (...args: any[]): {} }, V>(
      BaseClass: T,
      validate: (arg: V) => boolean
    ) {
      abstract class Validator extends BaseClass {
        value: V;
        validate(value: V) {
          return validate(value);
        }
      }
    
      return Validator;
    }
    
    class BaseInput {}
    
    class StringInput<T = string> extends ValidatorMixin(
      BaseInput,
      (value: T) => typeof value === 'string'
    ) {
    
    }
    
    // ValidatorValueType will be string | null | undefined
    type ValidatorValueType = StringInput<string | null | undefined>['value'];

    And now it has thrown an error. Yes there is a problem with extending static properties, but maybe you could make this check opinionated via additional flag like for strict generic

  5. locked and limited conversation to collaborators on Jul 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: Declaration EmitThe issue relates to the emission of d.ts filesFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions