storybook [Bug]: StoryFn类型在泛型为联合类型时中断(v7.0.0-beta.41)

camsedfj  于 7个月前  发布在  其他
关注(0)|答案(9)|浏览(63)

描述bug

如果TCmpOrArgs泛型包含联合类型,StoryFn类型在类型上的Assert不正确。

重现步骤

type A = {
  a: string;
}
type B = {
  b: string;
}
type C = A | B
const Test1: StoryFn<A> = (args) => { }; // args has correct type
const Test2: StoryFn<C> = (args) => { }; // args has 'any' type

系统信息

Environment Info:

  System:
    OS: Windows 10 10.0.19044
    CPU: (8) x64 Intel(R) Core(TM) i7-8665U CPU @ 1.90GHz
  Binaries:
    Node: 18.13.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.18.0 - C:\dev\cg-react-components\node_modules\.bin\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (109.0.1518.70)
  npmPackages:
    @storybook/addon-essentials: 7.0.0-beta.41 => 7.0.0-beta.41       
    @storybook/addon-links: 7.0.0-beta.41 => 7.0.0-beta.41 
    @storybook/addons: 7.0.0-beta.41 => 7.0.0-beta.41
    @storybook/react: 7.0.0-beta.41 => 7.0.0-beta.41
    @storybook/react-vite: 7.0.0-beta.41 => 7.0.0-beta.41
    @storybook/testing-react: 2.0.0-next.0 => 2.0.0-next.0
    @storybook/theming: 7.0.0-beta.41 => 7.0.0-beta.41

其他上下文信息

  • 无响应*
pn9klfpd

pn9klfpd1#

嘿,@dhulme,感谢你报告这个问题!这个方法在哪个版本上可以正常工作?
@kasperpeulen,有什么想法吗?

a11xaf1n

a11xaf1n2#

你好,它在6.5.15版本上运行正常(今天我从那个版本升级到了7)。

uinbv5nw

uinbv5nw3#

看起来条件类型使得Story类型具有分配性:
https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
@dhulme 你能检查一下在你的情况下,如果定义是这样写的,它是否能正确工作吗?

export type StoryFn<TCmpOrArgs = Args> = [TCmpOrArgs] extends [ComponentType<any>]
  ? AnnotatedStoryFn<ReactRenderer, ComponentProps<TCmpOrArgs>>
  : AnnotatedStoryFn<ReactRenderer, TCmpOrArgs>;

type A = {
  a: string;
};
type B = {
  b: string;
};
type C = A | B;

// args of type C
const Story: StoryFn<C> = (args) => null!;

它似乎对你的例子有效。

nhhxz33t

nhhxz33t4#

非常感谢@kasperpeulen,这解决了问题。然而,当我尝试从那个函数绑定一个新的函数时,我遇到了这个错误。我撤消了更改,但问题仍然存在,我认为今天早些时候在v7上没有出现这个问题——奇怪。

bfrts1fy

bfrts1fy5#

经过进一步的测试,确认它解决了这个问题。我看到的“类型注解是必要的”错误似乎存在于7.0.0-beta.41中,但在7.0.0-beta.40中可以正常工作。我可以为这个问题单独开一个问题吗?

au9on6nz

au9on6nz6#

"类型注解是必要的"错误似乎在较晚的版本中已经修复。@kasperpeulen ,我确认你的建议按预期工作。谢谢

ztyzrc3y

ztyzrc3y7#

感谢,已将其添加到下一个周期!

lstz6jyr

lstz6jyr8#

类型注解问题仍然存在于beta.47

The inferred type of 'Playground' cannot be named without a reference to '../../node_modules/@storybook/react/dist/types-0a347bb9.js'. This is likely not portable. A type annotation is necessary.
knpiaxh1

knpiaxh19#

它是由重命名引起的:

// types-0a347bb9.d.ts
export { ReactRenderer as R, StoryFnReactReturnType as S };

// index.d.ts
import { R as ReactRenderer, S as StoryFnReactReturnType } from './types-0a347bb9.js'

如果你移除它:

// types-0a347bb9.d.ts
export { ReactRenderer, StoryFnReactReturnType };

// index.d.ts
import { ReactRenderer, StoryFnReactReturnType } from './types-0a347bb9.js'

似乎可以正常工作。

相关问题