Как извлечь тип данных компонента Vue после инициализации с помощью служебного метода TypeScript? - PullRequest
0 голосов
/ 26 января 2020

У меня есть очень простой компонент, подобный этому:

const MyComponent = Vue.extend({
  data() {
    return {
      x: 0,
      y: 0
    };
  }
});

Я хочу получить такой тип данных:

type MyGoal = {
  x: number;
  y: number;
};

Я пробовал что-то, но не повезло, пожалуйста см. ниже код:

import Vue, { VueConstructor, ComponentOptions } from "vue";

const MyComponent = Vue.extend({
  data() {
    return {
      x: 0,
      y: 0
    };
  }
});

// Unfortunately, my utility is merging everybody together, it is impossible to extract the type anymore

type ExtractInstance<T> = T extends VueConstructor<infer V> // not working
  ? V
  : T extends ComponentOptions<infer V>
  ? V
  : never;

type Extracted = ExtractInstance<typeof MyComponent>;
// => type Extracted = {
// =>     x: number; // How can I get these guys?
// =>     y: number; //
// => } & Record<never, any> & Vue

enter image description here

Возможно ли это сделать без извлечения этой функции данных?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...