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

Father build类型生成问题 #713

Closed
liangskyli opened this issue Sep 12, 2023 · 11 comments · Fixed by #716
Closed

Father build类型生成问题 #713

liangskyli opened this issue Sep 12, 2023 · 11 comments · Fixed by #716

Comments

@liangskyli
Copy link

liangskyli commented Sep 12, 2023

版本 :
[email protected]
[email protected]
[email protected]

编译代码

type IFoo2Props = {
  /**
   * @description 标题
   * @default "默认值"
   */
  title: string;
};
const Foo2 = (props: IFoo2Props) => <h4>{props.title}</h4>;

export default Foo2;

出现编译错误

error - src/foo2/index.tsx:8:7 - TS2742: The inferred type of 'Foo2' cannot be named without a reference to 'packages/demo2/node_modules/@types/react/ts5.0/jsx-runtime'. This is likely not portable. A type annotation is necessary.
error - src/foo2/index.tsx:8:7 - TS2742: The inferred type of 'Foo2' cannot be named without a reference to 'packages/demo2/node_modules/@types/react/ts5.0/jsx-runtime'. This is likely not portable. A type annotation is necessary.
error - Error: Declaration generation failed.

编译代码改为FC类型,这样就编译正常

import type { FC } from 'react';

type IFoo2Props = {
  /**
   * @description 标题
   * @default "默认值"
   */
  title: string;
};
const Foo2: FC<IFoo2Props> = (props) => <h4>{props.title}</h4>;

export default Foo2;

期待:这两个写法应该都能正常编译成功。

@PeachScript
Copy link
Member

第二种才是正确写法,第一种直接跑 tsc 应该也会失败的

@PeachScript PeachScript closed this as not planned Won't fix, can't repro, duplicate, stale Sep 13, 2023
@liangskyli
Copy link
Author

第二种才是正确写法,第一种直接跑 tsc 应该也会失败的

tsc 编译正常啊!
image

@liangskyli
Copy link
Author

@umijs/max 构建也正常的

@PeachScript
Copy link
Member

@liangskyli 要用第一种声明方式的话,tsconfig.json 里的 jsx 配置需要改成 react-jsx;我这里试了下是可以构建成功的,我的步骤:

  1. npx create-dumi 选 react 模板
  2. src/Foo/index.tsx 换成你的写法
  3. tsconfig.json 的 jsx 换成 react-jsx
  4. npm run build
  5. cat dist/Foo/index.d.ts 可以看到用新版 jsx runtime 导出组件类型

如果你试下来仍然不行,可以提供最小可复现仓库看看

@liangskyli
Copy link
Author

liangskyli commented Sep 13, 2023

@PeachScript : tsconfig.json 的 jsx 是react-jsx,复现demo: https://github.com/liangskyli/dumi-lerna-demo/tree/farther-4.3.4-bug
安装:pnpm i
构建:pnpm build

@liangskyli
Copy link
Author

father": "4.3.1", 这个包就没问题

@PeachScript PeachScript reopened this Sep 15, 2023
@PeachScript
Copy link
Member

复现仓库可以复现,但和 tsc 行为不一致的原因尚未查明,哪怕按照 TypeScript 官方指南创建一个最简 compiler 也会报错:https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler

@PeachScript
Copy link
Member

原因已找到,下个版本修复,详见 #716

另外回到复现项目本身,monorepo 下建议子包都各自放独立的 tsconfig.json,可以继承根目录的内容但 include 需要仅包含子包文件,这会使得子包构建时 tsc include 的文件数量大幅减少、提升构建效率,同时也可以避免在现有 father 版本中出现该问题

@liangskyli
Copy link
Author

好,demo里子包没加独立的 tsconfig.json,正式的库会加,多谢建议

@PeachScript
Copy link
Member

v4.3.5 已发布

@PIMBA
Copy link

PIMBA commented Jun 27, 2024

there's still some error occurs when typescript > 5.4:
npm run build:

error - src/index.ts:17:5 - TS18047: 'value' is possibly 'null'.
error - src/index.ts:17:11 - TS2339: Property 'forEach' does not exist on type 'string | number | boolean | TypedJsonValue[] | TypedJsonValueObject'.
  Property 'forEach' does not exist on type 'string'.

npx tsc -w --noEmit

Found 0 errors. Watching for file changes.

try code below:

export type TypedJsonValue =
  | { type: "null"; value: null }
  | { type: "array"; value: number[] };

for (const [{ type, value }] of [] as [TypedJsonValue][])
  if (type === "array") value.forEach(() => {});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants