question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript does not set function name for MethodDeclarations

See original GitHub issue

This issue has been asked as a question on StackOverflow.

I have a TypeScript class, and I’m doing some metaprogramming where I need to be able to access instance.func.name, however TypeScript omits the function name in the compiled JS.

TypeScript:

class ClassName {
    // ... 
    func(): ReturnType {
        // ...
    }
}

Compiled JavaScript:

// ...
ClassName.prototype.func = function () {
    // ... 
};

Desired JavaScript:

ClassName.prototype.func = function func() {
    // ...                          ^^^^
};

Not sure if this is a bug or by design, but the source of the result is emitter.ts.

A proposed solution on StackOverflow was:

// src/compiler/emitter.ts:4671
function shouldEmitFunctionName(node) {
    if (node.kind === 173 /* FunctionExpression */) {
        // Emit name if one is present
        return !!node.name;
    }
    if (node.kind === 213 /* FunctionDeclaration */) {
        // Emit name if one is present, or emit generated name in down-level case (for export default case)
        return !!node.name || languageVersion < 2 /* ES6 */;
    }

    // MODIFIED
    if (node.kind === 143 /* MethodDeclaration */) {                    
        return true;
    }                                                        
}

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:9
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
jannesieracommented, Jun 20, 2018

Bump

5reactions
Manish3177commented, Jun 18, 2018

Hello, are there any updates on this issue? According to the latest activity above (on 8/31/2017), it was supposed to be added to TypeScript 2.6 but it isn’t there in it. Also, if it’s not too late, I would like to suggest that the name should include the class name as well (ClassName$functionName, for example). In large projects where the same function is used by several classes (for example when overriding a function from a base class), the function name alone isn’t sufficient to uniquely identify the actual function. It would be even better if there was a way to control the prefix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript not providing function name - Stack Overflow
Edit: TypeScript compiler does not write the function name because there is no handling for SyntaxKind.MethodDeclaration in emitter.ts:
Read more >
Functions - TypeScript: Handbook
Functions are the fundamental building block of any application in JavaScript. They're how you build up layers of abstraction, mimicking classes, ...
Read more >
Function.prototype.name - JavaScript - MDN Web Docs
Warning: JavaScript will set the function's name property only if a function does not have an own property called name . However, classes' ......
Read more >
How To Use Functions in TypeScript - DigitalOcean
If you do not wish to create a TypeScript environment on your local ... that TypeScript has implied its type from the function...
Read more >
Google JavaScript Style Guide
All goog.module files must declare exactly one goog.module name on a single ... It does not create the module ID as a globally...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found