Skip to content

Visitor throws Cannot read property 'kind' of undefined using [email protected]Β #38558

Description

@lukaskl

TypeScript Version:

  • πŸ”΄3.9.2
  • πŸ”΄3.9.1-rc
  • πŸ”΄3.9.0-beta
  • βœ…3.8.3 (it work with previous versions)

Search Terms:
Visitor TypeError kind undefined 85032

Code

Unknown (Error is thrown at node_modules/typescript/lib/typescript.js:85032:35)

But this part triggers it

export interface ColorProps {
  style?: CSSProperties
}

const Color = styled(({ style }: ColorProps) => (
  <div
    style={{
      ...style,
      background: `anything`,
    }}
  />
))``

Expected behavior:

It compiles

Actual behavior:

    TypeError: Cannot read property 'kind' of undefined

      at visitObjectLiteralExpression (node_modules/typescript/lib/typescript.js:85032:35)
      at visitJavaScript (node_modules/typescript/lib/typescript.js:83441:28)
      at visitor (node_modules/typescript/lib/typescript.js:83384:24)
      at visitNode (node_modules/typescript/lib/typescript.js:74768:23)
      at Object.visitEachChild (node_modules/typescript/lib/typescript.js:75209:108)
      at visitJavaScript (node_modules/typescript/lib/typescript.js:83491:31)
      at visitor (node_modules/typescript/lib/typescript.js:83384:24)
      at visitNodes (node_modules/typescript/lib/typescript.js:74819:48)
      at Object.visitEachChild (node_modules/typescript/lib/typescript.js:75034:53)
      at visitObjectLiteralExpression (node_modules/typescript/lib/typescript.js:85057:23)

Playground Link:

Related Issues:

Activity

  1. lissein commented on May 14, 2020

    @lissein
    Contributor

    I managed to reduce the reproduction code down to:

    const f = (props: any) => (...args: any) => ''; 
    
    f({
        background: `${1} anything`,
        ...{ test: 1 },
    })``

    The issue is in the es2015 transformer so any target above es2015 will be fine. In fact, the issue is in the object literal visitor when the node contains "es2015 features (eg. template literals, arrow functions, ...)" that need to be transformed. So you can reproduce the same crash with this code using arrow functions:

    const f = (props: any) => (...args: any) => ''; 
    
    f({
        background: (() => 1)(),
        ...{ test: 1 },
    })``

    This code, for instance does not crash because the object literal doesn't have any es2015 features in it.

    const f = (props: any) => (...args: any) => ''; 
    
    f({
        background: 'anything',
        ...{ test: 1 },
    })``

    I'll try to fix this.


    Until the bug is fixed, Lukas Klusis (@lukaskl) you can use a string instead of a template literal for the background variable or use an external variable to store the template literal outside of the object literal like that:

    export interface ColorProps {
      style?: CSSProperties
    }
    
    const Color = styled(({ style }: ColorProps) => {
      const background = `${1 + 1}`;
      return (
        <div
          style={{
            ...style,
            background,
          }}
        />
      );
    })``
  2. lukaskl commented on May 14, 2020

    @lukaskl
    Author

    Thank you Lissein (@lissein) for investigating this!

    To add another workaround, it is also possible to split this into two definitions, e.g.:

    const ColorUnstyled = ({ style }: ColorProps) => (
      <div
        style={{
          ...style,
          background: `anything`,
        }}
      />
    )
    
    const Color = styled(ColorUnstyled)``
  3. lissein commented on May 14, 2020

    @lissein
    Contributor

    After more investigations, I confirmed that the code crashing is in the es2015 transformer but the root cause is not in here. The bug is actually in the es2018 transformer.
    Here is an example of code with a target es2017 (< es2018):

    const f = (props: any) => (...args: any) => ''; 
    
    f({
        background: (() => 1)(),
        ...{ test: 1 },
    })``
    
    const a = {
        ...{ test: 1 },
    }
    "use strict";
    const f = (props) => (...args) => '';
    f({
        background: (() => 1)(),
        ...{ test: 1 },
    }) ``;
    const a = Object.assign({ test: 1 });

    As you can see, the second spread is transpiled correctly but the first one isn't.

  4. elibarzilay commented on May 15, 2020

    @elibarzilay
    Contributor

    (This is most likely very unrelated to #38383...)

  5. added 2 commits that reference this issue on May 15, 2020
    f8d63fb
    ea1e0ab
  6. added a commit that references this issue on May 15, 2020
    33c3e9e
  7. chaselal commented on Jul 21, 2020

    @chaselal

    tsc throws the same error for me with TypeScript version 3.9.7:

    Chase@DESKTOP-P21MP39 MINGW64 ~/company/companyWeb/projects/company.Server.Web ((b99b96a0de...))
    $ node_modules/typescript/bin/tsc
    C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:82931
                    throw e;
                    ^
    
    TypeError: Cannot read property 'kind' of undefined
        at visitObjectLiteralExpression (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:70631:35)
        at visitJavaScript (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:69611:28)
        at visitor (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:69554:24)
        at visitNode (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:63010:23)
        at Object.visitEachChild (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:63260:45)
        at visitParenthesizedExpression (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:70365:23)
        at visitJavaScript (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:69625:28)
        at visitor (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:69554:24)
        at Object.visitNode (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:63010:23)
        at transformFunctionBody (C:\Users\Chase\company\companyWeb\projects\company.Server.Web\node_modules\typescript\lib\tsc.js:70307:37)
    

    My project is quite large. I don't know which code tsc fails on.

    Edit: This happens when I try to upgrade directly from typescript version 3.7.3 to 3.9.7. Version 3.7.3 works for me.

  8. peterj35-dd commented on Aug 11, 2021

    @peterj35-dd

    Why has this issue been closed, what is the resolution without having to make manual code changes? I'm facing the same issue on 3.9.10, targetting ES5

  9. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions