developit / developit/preact-markup
Usage in a preact/compat codebase with TypeScript
- 主要语言
- JavaScript
- 星标
- 207
- 派生
- 27
- PR 合并指标
- 30 天内没有已合并 PR
描述
This might not be directly an issue, but I'm looking for some advice.
I'm trying to use this package (`preact-markup@2.1.1`) in a Next.js codebase set up with `preact/compat` as detailed in the [using-preact](https://github.com/vercel/next.js/tree/canary/examples/using-preact) example. I've also got TypeScript configured, with the following `tsconfig`:
```json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
},
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext", "ESNext.Intl", "ES2018.Intl"],
"allowJs": false,
"skipLibCheck": false,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"typeRoots": ["node_modules/@types", "./src/types"]
},
"exclude": ["node_modules", ".next", "out"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"]
}
```
I haven't followed the advice to add `"jsxImportSource": "preact",` from the Preact TS docs as that causes hundreds of errors in our codebase. I'm guessing that would be beneficial in a 'pure' Preact codebase, but isn't useful here. (If you can confirm this hunch, that would be great).
Now, the issue here is that, when I try to use `` in my JSX, I'm getting the following TS error:
```
'Markup' cannot be used as a JSX component.
Its instance type 'Markup' is not a valid JSX element.
Property 'refs' is missing in type 'Markup' but required in type 'ElementClass'.ts(2786)
```
`ElementClass` is defined in `@types/react`, likely because the TS compiler (erroneously) figures that (the return value of) any component used in JSX should extend `JSX.ElementClass`. I'm by no means a very experienced TS user, so this is a best guess.
Now, I've tried to monkey patch the type of `Markup` like this by overwriting the keys the TS compiler complains about in my own code:
```ts
import BaseMarkup from 'preact-markup';
import { ReactInstance } from 'react';
const Markup = BaseMarkup as typeof BaseMarkup & {
refs: { [key: string]: ReactInstance };
};
export { Markup };
```
Unfortunately, that doesn't work either as it doesn't seem to be possible to extend classes like this. Now I could probably just go `const Markup = BaseMarkup as any;` and call it a day, but I would like to find a way to accurately type this for my codebase, if at all possible so we can keep code completion in our editors. Is it possible to get this typed correctly somehow?
贡献指南
这个仓库没有索引到贡献指南
调研方向
使用 preact/compat 和提供的 tsconfig 在所描述的 Next.js 项目中重现该错误。结合检查 @types/react 的 JSX ElementClass 定义,检查 preact-markup 的 TypeScript 接口;当 Markup 可以在 JSX 中使用且不会出现所报告的类型错误,同时保留编辑器补全时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- next.js, react, typescript
- 领域
- frontend
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100