Skip to content
This repository has been archived by the owner on Jan 26, 2019. It is now read-only.

key on React Component does not compile #300

Closed
jlguenego opened this issue Apr 9, 2018 · 7 comments
Closed

key on React Component does not compile #300

jlguenego opened this issue Apr 9, 2018 · 7 comments

Comments

@jlguenego
Copy link

Is this a bug report?

Yes

Can you also reproduce the problem with npm 4.x?

Yes

Which terms did you search for in User Guide?

key
Module build failed: TypeError: Cannot read property 'length' of undefined

Environment

D:\repos\zzz_react_ts>npm ls react-scripts-ts
zzz_react_ts@0.1.0 D:\repos\zzz_react_ts
`-- react-scripts-ts@2.14.0

D:\repos\zzz_react_ts>node -v
v9.4.0

D:\repos\zzz_react_ts>npm -v
4.6.1

Then, specify:

  1. Operating system: Windows 10
  2. Browser and version (if relevant):
    Chrome 65

Steps to Reproduce

npm install -g create-react-app
create-react-app zzz --scripts-version=react-scripts-ts
cd zzz
npm start

(Write your steps here:)

  1. Replace App.tsx with the following content:
import * as React from 'react';
import './App.css';

const MyComp = () => <div>bar</div>;

class App extends React.Component {
  render() {
    return (
      <div>
        {[0, 1, 2].map(n => <MyComp key={n}/>)}
      </div>
    );
  }
}

export default App;

Expected Behavior

You should see no error in the browser, and a loop printed.

Actual Behavior

Error in the browser:
./src/App.tsx
Module build failed: TypeError: Cannot read property 'length' of undefined

The bug is on MyComp key={n}

Reproducible Demo

zzz_react_ts.zip

@tsiq-swyx
Copy link

tsiq-swyx commented Apr 9, 2018 via email

@wmonk
Copy link
Owner

wmonk commented Apr 9, 2018

Does key not have to be a string. https://reactjs.org/docs/lists-and-keys.html#keys

@jlguenego
Copy link
Author

It would be good if it was because of this. But unfortunately not ;)

@jlguenego
Copy link
Author

@tsiq-swyx Unfortunately the compiler seems to be very silent. Please try the example in the zip ;)

@tsiq-swyx
Copy link

i tried it and i can replicate the error. the problem looks like it is inside webpackhotdev client

image

it is probably something to do with the context

image

also even when you use a string it doesnt work

<div>{['a', 'b'].map(i => <MyComp key={i}/>)}</div>;

so either the key or the map is currently broken in this version 2.14.0. im not a maintainer here so i dont have any other info.

@DorianGrey
Copy link
Collaborator

On first build, you will get additional details in the browser console like this:

Uncaught Error: Module build failed: TypeError: Cannot read property 'length' of undefined
    at Object.unescapeLeadingUnderscores (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:12276)
    at checkJsxAttributesAssignableToTagNameAttributes (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:40111)
    at checkJsxOpeningLikeElementOrOpeningFragment (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:40048)
    at checkJsxSelfClosingElement (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:39445)
    at checkExpressionWorker (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:43631)
    at checkExpression (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:43524)
    at checkExpressionCached (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:43390)
    at getReturnTypeFromBody (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:42342)
    at getReturnTypeOfSignature (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:31758)
    at checkFunctionExpressionOrObjectLiteralMethodDeferred (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:42626)
    at checkDeferredNodes (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:47854)
    at checkSourceFileWorker (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:47890)
    at checkSourceFile (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:47868)
    at Object.forEach (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:1762)
    at getDiagnosticsWorker (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:47951)
    at getDiagnostics (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:47919)
    at Object.unescapeLeadingUnderscores (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:12276)
    at checkJsxAttributesAssignableToTagNameAttributes (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:40111)
    at checkJsxOpeningLikeElementOrOpeningFragment (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:40048)
    at checkJsxSelfClosingElement (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:39445)
    at checkExpressionWorker (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:43631)
    at checkExpression (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:43524)
    at checkExpressionCached (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:43390)
    at getReturnTypeFromBody (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:42342)
    at getReturnTypeOfSignature (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:31758)
    at checkFunctionExpressionOrObjectLiteralMethodDeferred (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:42626)
    at checkDeferredNodes (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:47854)
    at checkSourceFileWorker (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:47890)
    at checkSourceFile (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:47868)
    at Object.forEach (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:1762)
    at getDiagnosticsWorker (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:47951)
    at getDiagnostics (:3000/home/linne/Projects/zzz_react_ts/node_modules/typescript/lib/typescript.js:47919)
    at Object../src/App.tsx (module.js:22)
    at __webpack_require__ (bootstrap f88f4adc062c2410f094:678)
    at fn (bootstrap f88f4adc062c2410f094:88)
    at Object../src/index.tsx (index.tsx:1)
    at __webpack_require__ (bootstrap f88f4adc062c2410f094:678)
    at fn (bootstrap f88f4adc062c2410f094:88)
    at Object.0 (registerServiceWorker.ts:114)
    at __webpack_require__ (bootstrap f88f4adc062c2410f094:678)
    at ./node_modules/ansi-regex/index.js.module.exports (bootstrap f88f4adc062c2410f094:724)
    at bootstrap f88f4adc062c2410f094:724

This seems to be a regression introduced in typescript 2.8:
microsoft/TypeScript#22948
Seems to be fixed already, though there was no release yet including that fix (is tagged with 2.8.2, but that version is not on npm).

The same code works fine in 2.7.2. If you feel a bit adventurous, you might as well try 2.8.3-insiders.20180407.

@jlguenego
Copy link
Author

I agree with this analyze.

Temporary fix:
in the project we just need to replace typescript 2.8 with the old one (2.7.2) and it works.

D:\repos\zzz_react_ts>npm i typescript@2.7.2

Thanks guys you're genious and made my day !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants