preact icon indicating copy to clipboard operation
preact copied to clipboard

refs is missing in declaration file

Open ccpu opened this issue 7 years ago • 26 comments

I have noticed that having both preact and react types (@types/react) will confuse the typescript compiler and throw the following error:

'Property 'refs' is missing in type 'component'.'

apparently, the property 'refs' is missing from the 'preact.d.ts' declaration file.

adding the following code after line 101 in 'preact.d.ts' will solve the problem:

refs: { [key: string]: Component<any> | Element; };

tried to submit a PR, but couldn't find the file mentioned.

Thanks

ccpu avatar Sep 08 '18 18:09 ccpu

What is the reason for using both type definitions at the same time?

developit avatar Sep 12 '18 13:09 developit

we are using Lerna for creating an application with multiple packages, most of the packages are using react, and we are going to migrate them to Preact, they all using same 'node_modules' and that's why it confuses the compiler.

also to mention that it is not necessary to fix this problem, as we will eventually migrate to preact. but what would be a reason for not having the 'refs' in declaration file?

ccpu avatar Sep 12 '18 15:09 ccpu

I stumbled into the same problem using preact and react-redux. connect from react-redux uses reacts type definitions and expects my component to have refs. Is downgrading to some non-latest version of react-redux an option?

kirilloid avatar Oct 04 '18 11:10 kirilloid

I ran into exactly same problem. I'm not using @types/react. I could compile but VS code says it's error. @Cyrus-d 's solution helped me.

koukimetal avatar Oct 16 '18 12:10 koukimetal

I ran into same problem. I'm using @types/flux, it installs @types/react to my node_modules and conflicts to preact.

weijarz avatar Oct 25 '18 04:10 weijarz

I ran into same problem. I'm using @types/react-beautiful-dnd and @types/react-redux, it installs @types/react.

I think @developit should reopen this issue

dumistoklus avatar Nov 28 '18 21:11 dumistoklus

For reference, installing storybook types will also install React types and confuse the compiler. If you're using Babel 7 you should switch to the TypeScript Babel plugin which simply doesn't check for types on build, or do without Storybook types.

SolarLiner avatar Jan 09 '19 19:01 SolarLiner

@developit would you accept a fix for this? It'd be nice to hear... something from you, since this is affecting so many TypeScript users.

Here's what you get when you 'intermingle' Preact-typed and React-typed components.

image

resynth1943 avatar Jan 14 '21 23:01 resynth1943

Any updates on this?

darklight9811 avatar Mar 08 '21 22:03 darklight9811

Yeah having the same issue

JSH32 avatar Mar 12 '21 16:03 JSH32

What version of Preact are you using? On master I'm seeing all references to the ref property marked as optional which I think would fix this issue.

andrewiggins avatar Mar 13 '21 17:03 andrewiggins

Hit this too. @andrewiggins it's complaining about refs (plural) which doesn't exist in preact's type file

gpoitch avatar Apr 19 '21 19:04 gpoitch

Oh! refs with an s! 🤦‍♂️ My mistake - thanks for guiding me through that lol.

I see. This is referring to the legacy string refs feature that Preact 10 doesn't have built in support for. Need to dig more into this and see what we can do.

andrewiggins avatar Apr 19 '21 19:04 andrewiggins

@andrewiggins, shouldnt this issue be reopened then?

darklight9811 avatar Apr 19 '21 19:04 darklight9811

Can someone help me with a minimal repo? Or perhaps modify my attempt to reproduce the error? I tried to get one here with react-redux but TypeScript didn't throw any errors: https://gist.github.com/andrewiggins/f16d86f60a98a4f58b5a0facb64b3f74 I'm sure I'm missing something simple....

Do be sure to share your source, package.json, and tsconfig file.

andrewiggins avatar Apr 19 '21 22:04 andrewiggins

@andrewiggins New to using gists, but I've reproduced the problem using your gist as a starting point. I was seeing the "refs" issue when implementing the dropdownRender property of antd's Select component. https://gist.github.com/mellis481/517fb15b186b1d5d3aa494ce0f453517

You should see the issue on line 38.

CC: @marvinhagemeister

mellis481 avatar Apr 30 '21 13:04 mellis481

Also experiencing this issue- in my case there is a Property 'refs' is missing error when I try to use Enzyme to shallow or mount render any component, which I would expect would be a fairly common use case.

cxreiff avatar Jun 04 '21 16:06 cxreiff

Same problem. Any news ?

pierre-H avatar Aug 11 '21 11:08 pierre-H

Started having this problem after adding https://github.com/uber/baseweb ans https://github.com/styletron/styletron to my project. Any news?

ivanbrykov avatar Aug 16 '21 02:08 ivanbrykov

+1 Having the same problem with preact-router:

image

Pringels avatar Oct 07 '21 09:10 Pringels

Can someone help me with a minimal repo? Or perhaps modify my attempt to reproduce the error? I tried to get one here with react-redux but TypeScript didn't throw any errors: https://gist.github.com/andrewiggins/f16d86f60a98a4f58b5a0facb64b3f74 I'm sure I'm missing something simple....

Do be sure to share your source, package.json, and tsconfig file.

Well, I don't have a minimal repo, but I have the issue in my repository.

Ping yarn start to see the following errors.

ERROR in /home/homk/prg/snowinmars/aristotle.paraphrase/src/fe/src/components/App/App.tsx(28,8):
TS2786: 'Suspense' cannot be used as a JSX component.
  Its instance type 'Suspense' is not a valid JSX element.
    Property 'refs' is missing in type 'Suspense' but required in type 'ElementClass'.
ERROR in /home/homk/prg/snowinmars/aristotle.paraphrase/src/fe/src/components/App/App.tsx(29,10):
TS2786: 'Router' cannot be used as a JSX component.
  Its instance type 'Router' is not a valid JSX element.
    Property 'refs' is missing in type 'Router' but required in type 'ElementClass'.
Version: typescript 4.5.5

And it goes with runtime js error: Cannot read properties of undefined (reading 'replace') at at d (preact-router.module.js:1) on function function d(n){return n.replace(/(^\/+|\/+$)/g,"").split("/")}

I made a workaround by using react-router and non-lazy imports. For some reasons, if I import components with lazy(), it cause a runtime error.

snowinmars avatar Feb 01 '22 20:02 snowinmars

Same issue here :(

firstdorsal avatar Mar 16 '22 13:03 firstdorsal

Same.

preact + preact-rotuter + theme-ui

TicTak21 avatar May 05 '22 14:05 TicTak21

A potential workaround for this:

preact.d.ts

namespace preact {
  interface Component {
    // This is a workaround for https://github.com/preactjs/preact/issues/1206
    refs: Record<string, any>;
  }
}

declare module "preact" {
  export = preact;
}

memikri avatar May 16 '22 00:05 memikri

I was having this issue with Fragment, and adding this to preact.d.ts workfed for me:

export type Fragment = preact.JSX.Element

bdombro avatar Jun 18 '22 06:06 bdombro

A potential workaround for this:

preact.d.ts

namespace preact {
  interface Component {
    // This is a workaround for https://github.com/preactjs/preact/issues/1206
    refs: Record<string, any>;
  }
}

declare module "preact" {
  export = preact;
}

No, use this:

global.d.ts

export {};

declare global {
  namespace preact {
    interface Component {
      // This is a workaround for https://github.com/preactjs/preact/issues/1206
      refs: Record<string, any>;
    }
  }
}

The method above will override all typings..

DerGoogler avatar Jul 08 '22 18:07 DerGoogler

A good minimal examples for this problem is the typescript example in the enzyme-adapter-preact-pure module. It builds and runs fine in a terminal, but fails to do so in VS Code.

Side note: the file structure in preact has changed meanwhile and the simplest workaround I found was to add a ref definition in the Component interface in node_modules/preact/src/index.d.ts

    componentDidCatch?(error: any, errorInfo: ErrorInfo): void;
    refs: { [key: string]: Component<any> | Element; };

@developit Another reason for mixing the types is when using a library (like enzyme) which was originally written for React.

mike-lischke avatar Dec 09 '22 10:12 mike-lischke