question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to properly alias Preact/compat with TypeScript?

See original GitHub issue

Reproducing

  1. git clone https://github.com/talentlessguy/preact-emotion-ts.git
  2. yarn install
  3. Open vscode, with these settings:
{
    "eslint.packageManager": "yarn",
    "npm.packageManager": "yarn",
    "editor.formatOnSave": true,
    "typescript.updateImportsOnFileMove.enabled": "always",
    "editor.suggestSelection": "first",
    "eslint.autoFixOnSave": true,
    "eslint.alwaysShowStatus": true,
    "prettier.configPath": "/home/v1rtl/.prettierrc.yml",
    "[typescriptreact]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    }

And see this error on <Header> component:

JSX element type 'Header' does not have any construct or call signatures.

Versions

emotion: 10.0.22 preact: 10.0.5 eslint: 6.7 typescript: 3.7.2

Screenshot

image

The weirdiest thing is that npx eslint doesn’t show anything like this

preact-emotion-ts is 📦 v1.0.0 via ⬢ v13.2.0 
❯ npx eslint src/*.tsx --fix
Warning: React version was set to "detect" in eslint-plugin-react settings, but the "react" package is not installed. Assuming latest React version for linting.

I don’t what the issue is coming from but I suppose it is a problem with typings of preact (?)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
talentlessguycommented, Nov 26, 2019

@ForsakenHarmony thanks, it works now!

my final react.d.ts is like this:

import {
  AnyComponent,
  createElement,
  FunctionalComponent,
  RenderableProps,
  ComponentChild
} from 'preact'

export {
  Attributes,
  FunctionalComponent as SFC,
  AnyComponent as ComponentType,
  AnyComponent as JSXElementConstructor,
  Component as ComponentClass,
  ClassAttributes,
  PreactContext as Context,
  PreactProvider as Provider,
  VNode as ReactElement,
  VNode as ReactNode,
  createElement,
  Fragment,
  Ref,
  render,
  JSX,
  RenderableProps as ComponentPropsWithRef
} from 'preact'

// export type JSXElementConstructor = AnyComponent;

there are only 3 warnings about JSX namespace but VS Code doesn’t complain about them:

node_modules/@emotion/styled-base/types/helper.d.ts:7:19 - error TS2503: Cannot find namespace 'JSX'.

7   C extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any>
                    ~~~

node_modules/@emotion/styled-base/types/helper.d.ts:8:5 - error TS2503: Cannot find namespace 'JSX'.

8 > = JSX.LibraryManagedAttributes<C, React.ComponentPropsWithRef<C>>
      ~~~

node_modules/@emotion/styled-base/types/index.d.ts:29:16 - error TS2503: Cannot find namespace 'JSX'.

29 type JSXInEl = JSX.IntrinsicElements
                  ~~~


Found 3 errors.

Closing the issue 👍

4reactions
ForsakenHarmonycommented, Nov 25, 2019

I just remembered I used emotion at some point

https://github.com/ForsakenHarmony/sky1/blob/master/frontend/typings/react/index.d.ts I have react aliased with preact exports

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript | Preact: Fast 3kb React alternative with the same ...
TypeScript configuration; TypeScript preact/compat configuration; Typing components ... This way, your alias will work properly when libraries import React.
Read more >
react native - How to create typings alias in typescript the way ...
Take for example, preact is a lib with API highly resembles one of react . Say you write your code using react and...
Read more >
Module Resolution - Parcel
Aliases are supported through the alias field in package.json . This example aliases react to preact and some local custom module that is...
Read more >
Using Styled components with Preact | Momchil Georgiev
We need to do some “magic” called aliasing, so that when Styled looks for React it gets Preact compatibility instead.
Read more >
Building a Typescript Environment for Preact with Vite
Building a Typescript Preact environment using the No bundle tool Vite, along with ... It should work, but I'll add a few more...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found