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

Mixins lost the type information when type is generic. #289

Closed
hesi726 opened this issue Oct 31, 2018 · 5 comments
Closed

Mixins lost the type information when type is generic. #289

hesi726 opened this issue Oct 31, 2018 · 5 comments
Labels

Comments

@hesi726
Copy link

@hesi726 hesi726 commented Oct 31, 2018

I used the vue-property-decorator, it seems export mixins as Mixins.
So i asked there.

@Component
export class MyMixin extends Vue { }

@Component
export class YourMixin<T> extends Vue {  name: T; }

/** the followed code will error;
export class GenericMixin extends Mixins(TheirYourMixin<string>, MyMixin) {  
  setName(name: string) {
    this.name = name;
  }
}
**/

/** so i must write a non generic type for Mixins **/
@Component
export class TheirMixin extends YourMixin<string> {} 

export class GenericMixin<T> extends Mixins(TheirMixin, MyMixin) {  
  setName(name: string) {
    this.name = name;
  }
}

Is there any way to mixins other vue class but reserve the generic parameter?

@ktsn
Copy link
Member

@ktsn ktsn commented Nov 30, 2018

Unfortunately, I think there is no way to specify it with mixins helper as the mixin constructors are passed as value.

If anyone come up with the idea to handle this, feel free to put it this thread 🙂

@ktsn ktsn added the discussion label Nov 30, 2018
@garypippi
Copy link

@garypippi garypippi commented Mar 11, 2019

In recent version, passing type information to mixin helper like this worked.

import Vue from 'vue'
import Component, { mixins } from 'vue-class-component'

@Component
class MyMixin extends mixins<GenericMixin<string>>(GenericMixin) {
    created() {
        console.log(this.value('hello'))
    }
}

@Component
class GenericMixin<T=any> extends Vue {
    value(value: T): T {
        return value
    }
}
@marco-quintella
Copy link

@marco-quintella marco-quintella commented May 13, 2019

If think the only way to state the type you are expecting as @garypippi did. So the TS will surely know about what type it should expect since the generic parameter it's not the only one in the mixin types.

@liuyuan22
Copy link

@liuyuan22 liuyuan22 commented Nov 19, 2019

In recent version, passing type information to mixin helper like this worked.

import Vue from 'vue'
import Component, { mixins } from 'vue-class-component'

@Component
class MyMixin extends mixins<GenericMixin<string>>(GenericMixin) {
    created() {
        console.log(this.value('hello'))
    }
}

@Component
class GenericMixin<T=any> extends Vue {
    value(value: T): T {
        return value
    }
}

thanks~

@ktsn
Copy link
Member

@ktsn ktsn commented Apr 26, 2020

Closing as the solution is provided. Thank you for making this discussion!

@ktsn ktsn closed this Apr 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.