Skip to content

Faulty declarations file from extending generic base class #17805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
NoelAbrahams opened this issue Aug 15, 2017 · 4 comments
Closed

Faulty declarations file from extending generic base class #17805

NoelAbrahams opened this issue Aug 15, 2017 · 4 comments
Assignees
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Fixed A PR has been merged for this issue

Comments

@NoelAbrahams
Copy link

NoelAbrahams commented Aug 15, 2017

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'.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Aug 16, 2017
@mhegazy mhegazy added the Domain: Declaration Emit The issue relates to the emission of d.ts files label Sep 1, 2017
@mhegazy mhegazy modified the milestone: TypeScript 2.6 Sep 1, 2017
@mhegazy mhegazy modified the milestones: TypeScript 2.6, TypeScript 2.7 Oct 9, 2017
@mhegazy mhegazy modified the milestones: TypeScript 2.7, TypeScript 2.8 Jan 9, 2018
@mhegazy mhegazy modified the milestones: TypeScript 2.8, TypeScript 2.9 Mar 9, 2018
@weswigham
Copy link
Member

@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.

@weswigham weswigham added the Fixed A PR has been merged for this issue label Mar 20, 2018
@weswigham weswigham removed this from the TypeScript 2.9 milestone Mar 20, 2018
@weswigham weswigham assigned ahejlsberg and unassigned weswigham Mar 20, 2018
@weswigham
Copy link
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.

@NoelAbrahams
Copy link
Author

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

@mhegazy mhegazy added this to the TypeScript 2.9 milestone Mar 28, 2018
@mrazorvin
Copy link

mrazorvin commented Mar 30, 2018

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

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

6 participants