parcel icon indicating copy to clipboard operation
parcel copied to clipboard

[@parcel/transformer-typescript-types]: Meaningless "Got unexpected undefined" error message

Open jakubmazanec opened this issue 4 years ago • 15 comments

I encountered "Got unexpected undefined" error. I don't know what exactly causes it, but I created repo with minimal reproduction.

🎛 Configuration

package.json

{
  "name": "parcel-bug",
  "main": "dist/index.js",
  "source": "src/index.ts",
  "types": "dist/index.d.ts",
  "scripts": {
    "build": "parcel build"
  },
  "dependencies": {
    "next": "^12.0.10"
  },
  "devDependencies": {
    "@parcel/packager-ts": "^2.2.1",
    "@parcel/transformer-typescript-types": "^2.2.1",
    "parcel": "^2.2.1",
    "typescript": "^4.5.5"
  }
}

tsconfig.json

{
  "extends": "./tsconfig.options.json",
  "compilerOptions": {
    "composite": true,
    "esModuleInterop": true,
    "exactOptionalPropertyTypes": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "jsx": "react-jsx",
    "lib": ["DOM", "DOM.Iterable", "ES2021"],
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmit": true,
    "noImplicitOverride": true,
    "noImplicitReturns": true,
    "noUncheckedIndexedAccess": true,
    "rootDir": "./src",
    "strict": true,
    "target": "ES2021",
  },
  "include": ["src/**/*"],
  "references": []
}

Source code:

import type { NextWebVitalsMetric } from 'next/app';

declare module 'next/app' {
  export interface AppInitialProps {
    pageProps: unknown;
  }
}

export async function reportWebVitals({ value, name }: NextWebVitalsMetric) {}

🤔 Expected Behavior

Parcel builds with no error? I think this minimal reproduction shouldn't actually build (because of e.g. missing React), but the errors should be more descriptive and useful.

😯 Current Behavior

Parcel errors:

× Build failed.

@parcel/transformer-typescript-types: Got unexpected undefined

  Error: Got unexpected undefined
  at nullthrows (D:\projects\parcel-test\node_modules\nullthrows\nullthrows.js:7:15)
  at TSModuleGraph.propagate
  (D:\projects\parcel-test\node_modules\@parcel\transformer-typescript-types\lib\TSModuleGraph.js:271:48)
  at shake (D:\projects\parcel-test\node_modules\@parcel\transformer-typescript-types\lib\shake.js:42:35)
  at D:\projects\parcel-test\node_modules\@parcel\transformer-typescript-types\lib\TSTypesTransformer.js:139:33
  at transformation (D:\projects\parcel-test\node_modules\typescript\lib\typescript.js:107043:24)
  at transformRoot (D:\projects\parcel-test\node_modules\typescript\lib\typescript.js:107070:82)
  at Object.transformNodes (D:\projects\parcel-test\node_modules\typescript\lib\typescript.js:107054:78)
  at emitDeclarationFileOrBundle (D:\projects\parcel-test\node_modules\typescript\lib\typescript.js:107768:43)
  at emitSourceFileOrBundle (D:\projects\parcel-test\node_modules\typescript\lib\typescript.js:107671:13)
  at forEachEmittedFile (D:\projects\parcel-test\node_modules\typescript\lib\typescript.js:107397:30)

💻 Code Sample

Repo with reproduction. Clone and run npm install and npm run build.

🌍 Your Environment

Software Version(s)
Parcel 2.2.1
Node 16.13.2
npm 8.3.1
Operating System Windows 10 Enterprise 20H2

jakubmazanec avatar Feb 07 '22 23:02 jakubmazanec

https://github.com/parcel-bundler/parcel/blob/e294eafd9a49c056fb4b223c5ace5c0653428ede/packages/transformers/typescript-types/src/shake.js#L39

With the debugger, it looks like here is the node remains undefined.

zapplebee avatar Mar 15 '22 17:03 zapplebee

I also ran into this issue recently although slightly different

@parcel/transformer-typescript-types: Cannot read properties of undefined (reading 'unshift')
  TypeError: Cannot read properties of undefined (reading 'unshift')

the modifiers on the node in my case was undefined and I have found that this is only introduced when you have a typescript version >= 4.6.0.

@jakubmazanec I notice you have "typescript": "^4.5.5" can you take a look in your yarn.lock/package-lock.json and see what version is actually in your project? I suspect if you explicitly set a version like "typescript": "4.5.5" your problem might go away like mine did.

This does however not explain the problem since that is a minor version bump of TS and I would have expected a library depending on that to not be affected. If it is, then I think this lib needs a strict version associated to prevent this issue until the code causing the error is updated with the newer version of TS.

rathpc avatar Apr 04 '22 18:04 rathpc

@rathpc I reported this 2 months ago, when TypeScript 4.6 wasn't released yet. But I'll try anyway.

jakubmazanec avatar Apr 04 '22 22:04 jakubmazanec

I installed and run the repo with TypeScript 4.6.3 and also with TypeScript 4.5.5, same error.

Best if somebody took a look at that PR...

jakubmazanec avatar Apr 04 '22 22:04 jakubmazanec

I installed and run the repo with TypeScript 4.6.3 and also with TypeScript 4.5.5, same error.

Best if somebody took a look at that PR...

Ah ok, thanks for testing my theory at least!

rathpc avatar Apr 04 '22 23:04 rathpc

I got the same error. Don't know is it the same bug or not, but I figured out the following:

  1. Let's import some type from some npm package:
// index.ts
import { FC } from "react";
  1. Use this type and export:
// index.ts
export type MyComponent = FC<{}>;
  1. Create a file with the same name of the module in the first step:
// react.ts
export default "";
  1. Try to import something from the file that created in the third step:
// index.ts
import { FC } from "react";

// @parcel/transformer-typescript-types: Got unexpected undefined
import {} from "./react";

export type MyComponent = FC<{}>;

It also works with other modules. React is just an example.

Versions:

  • parcel: 2.5.0
  • @parcel/transformer-typescript-types: 2.5.0
  • @parcel/packager-ts: 2.5.0
  • typescript: 4.6.3
  • node: 17.8.0

Repo with reproduction: https://github.com/alexgraddev/parcel2-typescript-types-bug

graddotdev avatar Apr 22 '22 14:04 graddotdev

Would also appreciate a fix for this!

danieltroger avatar Aug 11 '22 16:08 danieltroger

I ran into this problem and figured out what the error means. So at least I can fix it in user-land code, but a more helpful message would be nice.

It happens when you get a squiggly line on a type import.

CleanShot 2022-08-25 at 15 42 34@2x

Swizec avatar Aug 25 '22 22:08 Swizec

Also looks like there's something with index re-exports

this doesn't work:

// src/index.ts
import { customTheme } from './themes'

// src/themes/index.ts
export * from './customTheme'

// src/themes/customTheme.ts
export const customTheme = ...

this works:

// src/index.ts
import { customTheme } from './themes/customTheme'

// src/themes/index.ts
export * from './customTheme'

// src/themes/customTheme.ts
export const customTheme = ...

Swizec avatar Aug 25 '22 23:08 Swizec

I had the same problem that was mentioned here https://github.com/parcel-bundler/parcel/issues/7669#issuecomment-1106567699, and found another issue when renaming it.

Name Works
react
_react
react_

It seems like leading _ is not welcome 🤔

ivanbanov avatar Sep 14 '22 09:09 ivanbanov

I'm having the same problem. Here is a reproducible repo. Would love if this could get resolved, or if anyone has any solutions. Thanks!

ted-palmer avatar Mar 01 '23 19:03 ted-palmer

I'm still seeing this every now and then, don't think it has been resolved

danieltroger avatar Aug 29 '23 07:08 danieltroger

I updated all dependencies in the repo with the bug reproduction; the bug still exists.

jakubmazanec avatar Oct 19 '23 22:10 jakubmazanec

I updated Parcel and TypeScript in the repo with the bug reproduction; the bug still exists.

jakubmazanec avatar Apr 17 '24 11:04 jakubmazanec