Skip to content

In JS, expando properties of a function are always treated as static methods #25906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sandersn opened this issue Jul 24, 2018 · 1 comment
Closed
Assignees
Labels
Bug A bug in TypeScript checkJs Relates to checking JavaScript using TypeScript

Comments

@sandersn
Copy link
Member

var assert = function() { };
assert.AssertionError = function (actual, expect) {
    this.name = 'AssertionError';
    this.actual = actual;
    this.expected = expect;
}

Expected behavior:
No errors, and this: AssertionError, and assert.AssertionError is a constructor function.

Actual behavior:
this: typeof assert, which is a function, so that, for example this.name tries to overwrite the built-in Function.name. Basically, assert.AssertionError behaves like a static method of assert instead of a static property that is a constructor function.

Note that usage of assert.AssertionError works as desired: you can say new assert.AssertionError().actual.

@sandersn
Copy link
Member Author

This works correctly if you put @constructor in front of assert.AssertionError. Since the usages are ambiguous, I think this it's OK to require an annotation. You could argue that the annotation be required in the opposite case because it's (possibly) less common, but JSDoc doesn't already have a tag for marking things as static methods, as far as I know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript checkJs Relates to checking JavaScript using TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants