Code Monkey home page Code Monkey logo

ts-define-class-field-bug's Introduction

This repo illustrates what I consider a bug in the typescript code generator for the useDefineForClassFields feature.

This has been tested in Node v18.

Node

Run npm run test:node to execute the plain node script.

➜  ts-define-class-field-bug git:(main) npm run test:node

> [email protected] test:node
> node test-node.js

/Users/jan/development/ts-define-class-field-bug/test-node.js:3
    throw new Error()
    ^

Error
    at A.foo (/Users/jan/development/ts-define-class-field-bug/test-node.js:3:11)
    at Object.<anonymous> (/Users/jan/development/ts-define-class-field-bug/test-node.js:10:3)
    at Module._compile (node:internal/modules/cjs/loader:1149:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1203:10)
    at Module.load (node:internal/modules/cjs/loader:1027:32)
    at Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.10.0

Observe that the name of the method that errored is reported as A.foo

Typescript with useDefineForClassFields

Run npm run test:useDefine to execute the Typescript version with the useDefineForClassFields flag

➜  ts-define-class-field-bug git:(main) ✗ npm run test:useDefine

> [email protected] test:useDefine
> tsc --useDefineForClassFields && node test-typescript.js

/Users/jan/development/ts-define-class-field-bug/test-typescript.js:9
                throw new Error();
                ^

Error
    at A.value (/Users/jan/development/ts-define-class-field-bug/test-typescript.js:9:23)
    at Object.<anonymous> (/Users/jan/development/ts-define-class-field-bug/test-typescript.js:22:3)
    at Module._compile (node:internal/modules/cjs/loader:1149:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1203:10)
    at Module.load (node:internal/modules/cjs/loader:1027:32)
    at Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.10.0

The Bug:

Observe that the name of the method that errored is reported as A.value.

The function object that Javascript creates gets the name of its value key in the property descriptor of defineProperty. This problem becomes quite annoying when used in the common pattern of pre-binding arrow-functions to class instances, and then passing them around as event handlers in the browser.

Stack traces and the Chromium flame graph are then full of methods named value.

ts-define-class-field-bug's People

Contributors

janv avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.