Skip to content
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

No autocompletions for Parent class when using ES2015 mixins (function returning the Class) #18732

Closed
kasperpeulen opened this issue Sep 25, 2017 · 9 comments
Labels
Domain: JavaScript The issue relates to JavaScript specifically Domain: JSDoc Relates to JSDoc parsing and type generation Fixed A PR has been merged for this issue

Comments

@kasperpeulen
Copy link

kasperpeulen commented Sep 25, 2017

Consider this code:

class Parent {
  parentMethod() {  }
}

const Mixin = Base => class Mixin extends Base {
  mixinMethod() {  }
}

class Child extends Mixin(Parent) {
  childMethod() {
    this.
  }
}

For the Child class, all three method are available, but typescript doesn't detect the parentMethod to be available.

image

@jwbay
Copy link
Contributor

jwbay commented Sep 25, 2017

There may be a better way to do this, but the main problem is that Base is of type any for your mixin. If you give it a generic type so it can flow through, parent methods will show up.

type Constructable = new (...args) => any;

class Parent {
  parentMethod() {  }
}

const Mixin = <T extends Constructable>(Base: T) => class Mixin extends Base {
  mixinMethod() {  }
}

class Child extends Mixin(Parent) {
  childMethod() {
    this. // all three show up here in playground
  }
}

@kasperpeulen
Copy link
Author

kasperpeulen commented Sep 25, 2017

Nice! I tried to implement this using jsdoc syntax, but without luck, any ideas?

/** @typedef {new (...args) => any} Constructable */

class Parent {
  parentMethod() {  }
}

/**
 * @template T extends Constructable
 * @param {T} Base 
 */
const Mixin = (Base) => class Mixin extends Base {
  mixinMethod() {  }
}

class Child extends Mixin(Parent) {
  childMethod() {
    this. // still only two autocompletions
  }
}

@DanielRosenwasser
Copy link
Member

I think the problem here is that TypeScript doesn't have a way of expressing constraints on type parameters in JSDoc.

In other words:

/**
 * @template T extends Constructable
 */

doesn't actually add a constraint.

@dimvar I see that you mentioned some work on bounded generics here for Closure. Any word on that?

@dimvar
Copy link

dimvar commented Sep 25, 2017

No progress on that yet.

@mhegazy mhegazy added Help Wanted You can do this Domain: JSDoc Relates to JSDoc parsing and type generation Revisit An issue worth coming back to and removed Help Wanted You can do this labels Sep 25, 2017
@akdor1154
Copy link

akdor1154 commented Oct 11, 2017

Is there anywhere that "types I can describe in TS but not JSDoc" (such as this) are listed?

@mhegazy mhegazy added the Salsa label Oct 11, 2017
@weswigham weswigham added Domain: JavaScript The issue relates to JavaScript specifically and removed Domain: JavaScript The issue relates to JavaScript specifically Salsa labels Nov 29, 2018
@ma2ciek
Copy link

ma2ciek commented Jan 17, 2019

Hi!

Did you achieve some progress on that field (I mean allowing constraints in the JSDoc @template)? It'd be really helpful as it would allow type checking JS code based on mixins (E.g. the https://github.com/ckeditor/ckeditor5 codebase).

In many scenarios, the previous approach (https://www.typescriptlang.org/docs/handbook/mixins.html) is not an option as it requires a massive code duplication (e.g. creating 15 fake properties and methods to satisfy interfaces).

@ma2ciek
Copy link

ma2ciek commented Jan 17, 2019

And I'd change the misleading title of the issue. When using --allowJs and --checkJs for the above code TS prints an error and there's no way to workaround it so IMO it's a bigger problem.

@RyanCavanaugh RyanCavanaugh added Fixed A PR has been merged for this issue and removed Revisit An issue worth coming back to labels Mar 7, 2019
@RyanCavanaugh
Copy link
Member

OP works in latest on Playground

@rampage1212
Copy link

@RyanCavanaugh
Please explain more details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: JavaScript The issue relates to JavaScript specifically Domain: JSDoc Relates to JSDoc parsing and type generation Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

10 participants