GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Type cast in one line arrow function `() => ({} as Type)` cause vite:esbuild transform error · Issue #348 · vitejs/vite-plugin-react

Type cast in one line arrow function () => ({} as Type) cause vite:esbuild transform error  #348

New issue
Copy link
New issue
Copy link
Closed
Closed
Type cast in one line arrow function () => ({} as Type) cause vite:esbuild transform error #348
Copy link
Labels
bug: upstreamBug in a dependency of Vite

Description

@daolanfler
daolanfler
opened on Jul 16, 2024
Issue body actions

Describe the bug

Type cast in useMemo () => ({} as Type) cause vite:esbuild transform error.

code:

  const v = useMemo(
    () =>
      ({
        name: 'random',
        age: count,
      } as RandomType), // type cast here will cause error
    [count]
  );

  // if not cast to `RandomType` in `()` return , its ok
  const v2 = useMemo<RandomType>(
    () => ({
      name: 'random',
      age: count,
    }),
    [count]
  );

error msg:

10:34:53 [vite] hmr update /src/App.tsx
10:34:53 [vite] Internal server error: Transform failed with 1 error:
/home/projects/vitejs-vite-l8ggg5/src/App.tsx:17:9: ERROR: Expected ";" but found ":"
  Plugin: vite:esbuild
  File: /home/projects/vitejs-vite-l8ggg5/src/App.tsx:17:11
  
  Expected ";" but found ":"
  15 |      {
  16 |        name: 'random',
  17 |        age: count
     |           ^
  18 |      } as RandomType, // type cast here will cause error
  19 |      [count]
  
      at failureErrorWithLog (/home/projects/vitejs-vite-l8ggg5/node_modules/esbuild/lib/main.js:1462:15)
      at eval (/home/projects/vitejs-vite-l8ggg5/node_modules/esbuild/lib/main.js:745:50)
      at responseCallbacks.<computed> (/home/projects/vitejs-vite-l8ggg5/node_modules/esbuild/lib/main.js:612:9)
      at handleIncomingPacket (/home/projects/vitejs-vite-l8ggg5/node_modules/esbuild/lib/main.js:667:12)
      at Socket.readFromStdout (/home/projects/vitejs-vite-l8ggg5/node_modules/esbuild/lib/main.js:590:7)
      at Socket.emit (node:events:30:10899)
      at addChunk (node:internal/streams/readable:225:3685)
      at readableAddChunk (node:internal/streams/readable:225:3393)
      at Readable.push (node:internal/streams/readable:225:4971)
      at _0x8d40b9.onStreamRead (node:internal/stream_base_commons:211:2596)

similar code like below doesn't cause error in vanilla vite, but errors with this plugin

  const fakeMemo = (fn: any, dep: []) => fn(dep);
  const v3 = fakeMemo(
    () =>
      ({
        name: 'random',
        age: count,
      } as RandomType),
    []
  );

Reproduction

https://stackblitz.com/edit/vitejs-vite-l8ggg5?file=src%2FApp.tsx

Steps to reproduce

Run npm install then npm run dev

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitejs/plugin-react: ^4.3.1 => 4.3.1 
    vite: ^5.3.2 => 5.3.3

Used Package Manager

npm

Logs

No response

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guidelines.
  • Read the docs.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Make sure this is a Vite issue and not a framework-specific issue.
  • Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
  • The provided reproduction is a minimal reproducible example of the bug.

Activity

  1. daolanfler
    added
    pending triage
    on Jul 16, 2024
  2. daolanfler
    changed the title [-]Type cast in useMemo `() => ({} as Type)` cause vite:esbuild transform error [/-] [+]Type cast in one line arrow function `() => ({} as Type)` cause vite:esbuild transform error [/+] on Jul 16, 2024
  3. ak-adversus commented on Jul 16, 2024

    @ak-adversus
    ak-adversus
    on Jul 16, 2024
    More actions

    This was caused by an issue in babel, which has been fixed by babel/babel#16648

    Reacted by Arnaud Barré and daolanfler
  4. ArnaudBarre
    added
    bug: upstreamBug in a dependency of Vite
    and removed
    pending triage
    on Jul 16, 2024
  5. ArnaudBarre
    closed this as completedon Jul 21, 2024
  6. github-actions
    locked and limited conversation to collaborators on Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug: upstreamBug in a dependency of Vite

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.