Current state of preact type definitions is not in very good shape and is impossible to use it with any React library.
Problems:
What needs to be done:
We are using Preact, Skate with Typescript at work ( large enterprise company ), and have to override everything manually, so everything works. With that said I can contribute to this project by setting up everything how it needs to be. Also we relly on Preact in upcoming SkateJS 5, and this is kind of blocker for us.
Hope this makes things clearer.
I'm all for this. Anything you think we can do to make that PR easier to dissect?
Does this issue and the PR #621 also fix following issue? I'm using preact and write my tests with enzyme (thankfully preact-compat-enzyme exists!). Unfortunately I get a lot of these errors as long as I add the @types/enzyme type definitions because they depend on @types/react:
node_modules/@types/react/index.d.ts(2727,47): error TS2403: Subsequent variable declarations must have the same type. Variable 'props' must be of type 'any', but here has type '{}'.
At the moment the only solution that I know is not to add @types/enzyme and make an own declaration declare module 'enzyme'; and have to work with any in my tests :confused:
Yes it solves everything. As I've said we are using custom type defs for preact and preact-compat at work, otherwise it won't work with react router, react forms, i18n-next, enzyme and so on in matter of compile time errors
I'm currently on Vacation, sorry for radio silence. I'll be back around May 8th so yeah, thx for patience โฎ๏ธโ๏ธ
no problem man ๐
hope you're somewhere warm haha
Haha yup, in Miami ;) :P
Cool! I'm really looking forward for this.
One little question:
As I've said we are using custom type defs for preact and preact-compat
How do you do that? Is there a TypeScript compiler option that says _"ignore the shipped .d.ts file from preact and use my own declarations file instead?"_ I was always searching for that and didn't find anything. The only thing that I know is simply not to add a @types/yourlibrary dependency and write my own for that. But for libraries that already ship with type declarations (like preact)? ๐ค
By the way: I wish you a nice and sunny vacation ๐ โ๏ธ
Is there anything I can do to help with this?
PR reviews would be a huge help here. There's so much value, but it's also a big change to wade through cause @Hotell works too fast ๐
Is #621 the canonical one? Also is there a goal of ever converting preact to TypeScript?
Converting preact itself to typescript is very very unlikely, since it would be a bit infeasible to produce output that small. Preact is basically written in ES5, just with let and const.
Converting preact itself to typescript is very very unlikely, since it would be a bit infeasible to produce output that small.
Why do you think that ? :) Typesript is just Javascript, so you get same output as with vanilla JS, no payload increase
Preact is basically written in ES5, just with let and const.
Yup and TS is again just JS, you can use only those Ecmascript features, which you need
Overall, if Preact wont use typed JS, I very highly recommend using Typescript in the background, because it is capable of checking your code without writing any TypeScript at all. #powerOverwhelming
You can also read about it here (just search for Errors in .js files with --checkJs). Just add checkJs to your tsconfig.json and you've got type check support "magically" in JS files ๐
I noticed that Preact and @types/react cannot be installed simultaneously. I assume this issue is the reason?
Repro steps: 1. create empty folder with tsconfig.json:
{"compilerOptions":{
"target": "es5",
"module": "umd",
"moduleResolution": "node",
"sourceMap": true,
"jsx": "react",
}}
~~~tsx
/** @jsx preact.h */
import * as preact from 'preact';
preact.render(
Run these commands (they all work fine):
npm init -y
npm install -g typescript
npm install preact
tsc
But with one more command...
npm install --save-dev @types/react
tsc goes insane:
app.tsx(5,3): error TS2559: Type '{ children: string; }' has no properties in common with type 'HTMLAttributes'.
node_modules/@types/react/index.d.ts(2239,47): error TS2717: Subsequent property declarations must have the same type. Property 'props' must be of type 'any', but here has type '{}'.
node_modules/@types/react/index.d.ts(2249,13): error TS2717: Subsequent property declarations must have the same type. Property 'a' must be of type 'HTMLAttributes', but here has type 'DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>'.
node_modules/@types/react/index.d.ts(2250,13): error TS2717: Subsequent property declarations must have the same type. Property 'abbr' must be of type 'HTMLAttributes', but here has type 'DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement>'.
node_modules/@types/react/index.d.ts(2251,13): error TS2717: Subsequent property declarations must have the same type. Property 'address' must be of type 'HTMLAttributes', but here has type 'DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement>'.
node_modules/@types/react/index.d.ts(2252,13): error TS2717: Subsequent property declarations must have the same type. Property 'area' must be of type 'HTMLAttributes', but here has type 'DetailedHTMLProps<AreaHTMLAttributes<HTMLAreaElement>, HTMLAreaElement>'.
*** ABOUT 150 MORE ERRORS ***
The errors start at line 2239 in @types/react when it tries to define JSX (declare global { namespace JSX { ...).
@qwertie No, this is a separate issue, see #1036. The reason they are clashing is that both define the JSX types globally. The recent TypeScript 2.8 release added support for per-file JSX types, but neither @types/react nor we do support that for now. We definitely intend to add support for that in the future though.
Closing this as the typings have been completely rewritten in 8.2.8
Using Preact 8.2.9.
Seeing all those messages.
@theKashey What kind of messages? That@types/react cannot be installed along preact or something else from this thread? The former should be tracked here #1036
Everything is about TS2717, and yes - I am mixing React and Preact - that is the problem.
@theKashey I've created this a while ago, so Preact is usable with Typescript and React packages without issues. You may find it useful https://github.com/Hotell/react-preact-typescript-interop
@Hotell You rock, that is really awesome ๐ Would you be interested in pushing some of your changes/learnings upstream?
Most helpful comment
Why do you think that ? :) Typesript is just Javascript, so you get same output as with vanilla JS, no payload increase
Yup and TS is again just JS, you can use only those Ecmascript features, which you need
Overall, if Preact wont use typed JS, I very highly recommend using Typescript in the background, because it is capable of checking your code without writing any TypeScript at all. #powerOverwhelming
https://twitter.com/martin_hotell/status/865982432763404288