storybook [Bug]: Vue-Typescript通用组件在CSF3中无法工作

mo49yndu  于 6个月前  发布在  TypeScript
关注(0)|答案(8)|浏览(68)

描述bug

我尝试在故事中使用泛型组件,但由于推断的类型错误而失败。
例如:

<script setup lang="ts" generic="TValue extends Array<number> = Array<number>">

export interface AreaChartProps<TValue extends Array<number> = Array<number>> {
  lineColor?: string;
  values: Array<TValue>;
...
}

而故事是:

import { AreaChart } from "@libs/charts/src";

import type { Meta, StoryObj } from "@storybook/vue3";

import type { ComponentProps } from "vue-component-type-helpers";

type ComponentType = ComponentProps<typeof AreaChart>;
type Story = StoryObj<ComponentType>;

const meta: Meta<ComponentType> = {
  component: AreaChart // <-- Error is here
};

export default meta;

TypeScript错误:

Type '<TValue extends number[] = number[]>(__VLS_props: AreaChartProps<TValue> & VNodeProps & AllowedComponentProps & ComponentCustomProps, __VLS_ctx?: Pick<...> | undefined, __VLS_expose?: ((exposed: {}) => void) | undefined, __VLS_setup?: Promise<...>) => VNode<...> & { ...; }' has no properties in common with type 'Omit<ConcreteComponent<AreaChartProps<number[]> & VNodeProps & AllowedComponentProps & ComponentCustomProps>, "props">'.

我尝试用这个更改故事的签名,但问题仍然存在:

type ComponentType = typeof AreaChart;
type Story = StoryObj<ComponentType>;

const meta: Meta<ComponentType> = {
  component: AreaChart,
};

export default meta;

我找到的唯一解决方法是简单地对元数据进行类型转换,但这样就失去了对 meta 对象的类型检查:

const meta = {
  component: AreaChart,
};

export default meta as Meta<ComponentType>;

是否可以在故事中使用泛型组件?

重现问题

如果你下载项目并从vscode打开它,问题就会显现出来:
https://stackblitz.com/edit/github-sgrypj?file=src%2Fstories%2FButton.stories.tsx

系统信息

Environment Info:

  System:
    OS: macOS 13.5.2
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 16.19.0 - ~/.nvm/versions/node/v16.19.0/bin/node
    npm: 8.19.3 - ~/.nvm/versions/node/v16.19.0/bin/npm
  Browsers:
    Chrome: 117.0.5938.88
    Edge: 114.0.1823.51
    Safari: 16.6
  npmPackages:
    @storybook/addon-a11y: ~7.4.2 => 7.4.2 
    @storybook/addon-actions: ~7.4.2 => 7.4.2 
    @storybook/addon-backgrounds: ~7.4.2 => 7.4.2 
    @storybook/addon-docs: ~7.4.2 => 7.4.2 
    @storybook/addon-essentials: ~7.4.2 => 7.4.2 
    @storybook/addon-interactions: ~7.4.2 => 7.4.2 
    @storybook/addon-links: ~7.4.2 => 7.4.2 
    @storybook/addon-measure: ~7.4.2 => 7.4.2 
    @storybook/addon-outline: ~7.4.2 => 7.4.2 
    @storybook/addons: ~7.4.2 => 7.4.2 
    @storybook/api: ~7.4.2 => 7.4.2 
    @storybook/channel-postmessage: ~7.4.2 => 7.4.2 
    @storybook/channel-websocket: ~7.4.2 => 7.4.2 
    @storybook/client-api: ~7.4.2 => 7.4.2 
    @storybook/components: ~7.4.2 => 7.4.2 
    @storybook/core-events: ~7.4.2 => 7.4.2 
    @storybook/manager-api: ~7.4.2 => 7.4.2 
    @storybook/preview-web: ~7.4.2 => 7.4.2 
    @storybook/testing-library: ~0.2.1 => 0.2.1 
    @storybook/theming: ~7.4.2 => 7.4.2 
    @storybook/vue3: ~7.4.2 => 7.4.2 
    @storybook/vue3-vite: ~7.4.2 => 7.4.2

其他上下文

  • 无响应*
gjmwrych

gjmwrych1#

我也遇到了使用泛型支持组件的问题,如果能得到一个解决这类情况的方案就太好了。

xv8emn3q

xv8emn3q2#

临时解决方案,我使用这种方法:

qyuhtwio

qyuhtwio3#

同样的问题,这个错误在普通组件中也会出现。

gudnpqoy

gudnpqoy4#

@kasperpeulen,请问这个问题出现的原因是什么?

n7taea2i

n7taea2i5#

嘿,伙计们,我也遇到了同样的问题。有人找到可接受的解决方法了吗?

nr7wwzry

nr7wwzry6#

React也存在同样的问题!

使用这种类型

通过在第12行添加临时解决方法,但它的行为类似于| string ...

ht4b089n

ht4b089n7#

如何临时解决方案,我使用这种方法

感谢,@Gumper-x。这对我有效。我能够调整它使其更严格(并删除 unknown 转换):

const meta = {
  title: 'MyComponent',
  component: MyComponent as Record<keyof typeof MyComponent, unknown>,
  // ...
} satisfies Meta<typeof MyComponent>;
mitkmikd

mitkmikd8#

I am using the following.

type GenericMeta<C> = Omit<Meta<C>, "component"> & {
  component: Record<keyof C, unknown>
}

const meta = {
  title: "MyComponent",
  component: MyComponent,
  args: {
    "modelValue": "string",
  },
  // ...
} satisfies GenericMeta<typeof MyComponent<string>>;

相关问题