preactjs / preact

⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
https://preactjs.com
MIT License
36.78k stars 1.96k forks source link

Rebuild `compat/src/index.d.ts` for compatibility with React. #2103

Open 38elements opened 4 years ago

38elements commented 4 years ago

IMHO, compat exists for compatibility for React. Therefore, the type definition which is provided by compat should be compatibility for React. compat/src/index.d.ts should give priority to compatibility over precise. src/internal.d.ts exists for Preact. src/internal.d.ts should give priority to precise over compatibility.

pmkroeker commented 4 years ago

The typings for compat were initially created for those wanting to use pieces from compat while in a preact only space (ie Suspense/Portal etc).

38elements commented 4 years ago

I think preact/compat is compatibility layer. I think the initally papose of typings for preact/compat is different from preact/compat. I think it needs compatibility, since it is the typings for compatibility layer.

pmkroeker commented 4 years ago

What changes would you propose? For those who directly import preact/compat in a TS project (without React) these typings are required.

Yes the main purpose of compat is for React compatibility, but it can also be used directly. If you are using React with a build system that aliases to preact/compat, the React typings should (and I believe are) used instead of the typings in preact/compat.

38elements commented 4 years ago

I am not familiar with preact/compat. I may be misunderstanding. I t seems that preact.VNode is incompatible with React one.

ForsakenHarmony commented 4 years ago

@38elements you're not expected to use the typings from both libraries

Ideally you'd want to alias it both for webpack and typescript

38elements commented 4 years ago

In the case using React library whose code creates VNode in TypeScript, I think it may raise error. This may be rare case.

pmkroeker commented 4 years ago

I recall testing having a mixed system of preact/React in Typescript and it worked as expected. Webpack did the aliasing to compat, but VSCode was showing React typings on React components and preact typings on preact ones with no problems (this was a little while ago but nothing fundamental has changed)

38elements commented 4 years ago

I think most React library does not have code which creates VNode (createElement() by one's own work), since they uses createElement() in React. May I confirm? In compat, Does it not raise the error like #1953?

https://github.com/preactjs/preact/blob/abc162c4d6442d5495f6aaba5d31c2bc75a3869b/src/index.d.ts#L42-L49

https://github.com/preactjs/preact/blob/abc162c4d6442d5495f6aaba5d31c2bc75a3869b/src/index.d.ts#L13-L30

https://github.com/preactjs/preact/blob/abc162c4d6442d5495f6aaba5d31c2bc75a3869b/compat/src/index.d.ts#L33

ReactElement https://github.com/DefinitelyTyped/DefinitelyTyped/blob/58c168c373aeb406972af7fdcdad2476abb9ae55/types/react/index.d.ts#L107-L111

ReactNode https://github.com/DefinitelyTyped/DefinitelyTyped/blob/58c168c373aeb406972af7fdcdad2476abb9ae55/types/react/index.d.ts#L200

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/58c168c373aeb406972af7fdcdad2476abb9ae55/types/react/index.d.ts#L463