-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Expando function declarations cant emit non-identifier properties #55190
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
Comments
I'm not sure but I guess it's because function declarations can merge with namespaces while function expressions cannot? Consider this code export function decl() {}
export namespace decl { export var foo = 1; }
decl['b-a-r'] = 1; What do you think it should emit? You may expect export declare const decl: {
(): void;
'b-a-r': number;
};
export declare namespace decl {
var foo: number;
} But it is not valid typescript since variables cannot merge with namespaces. Also, it should not be export declare const decl: {
(): void;
foo: number;
'b-a-r': number;
}; because it will prevent potential namespace merging in the future, for the same reason above. |
Yeah, I don't see what the proposed "expected" behavior is |
My wishy-washy answer is "the namespace merge feels more natural", but that's likely because I've seen it all over the DOM types. Although that does mean that it's likely better supported. |
The code author shouldn't have to know about the distinction between function declarations and function expressions in this regard. The expected result is that when declarations are emitted for this code then the same set of properties should be available for access on both exports. How the emitted At the very least I'd expect a diagnostic about those declarations not being "portable" to be raised. |
How come declare namespace bodyParser {
interface BodyParser { ... }
interface Options { ... }
}
declare const bodyParser: bodyParser.BodyParser;
export = bodyParser; EDIT: nvm, I figured it out, it's because the |
Some non-identifier cases could be emitted like this: export declare function decl(): void;
export declare namespace decl {
let x_1: number;
export { x_1 as "non identifier, hey I have whitespace and all" };
} That doesn't really solve the problem with symbols and numeric literals though. |
Bug Report
π Search Terms
expando properties function declaration symbol computed
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
π Expected behavior
I'd expect
decl
to be transformed into an object instead of a namespace. That would allow the same properties to be emitted fordecl
as the ones that are emitted forarrow
.The object variant was the first choice by @sandersn here but then it got changed to a namespace. I didn't find any reason given for this change/decision in the PR comments though. Is there anything that the namespace can deliver here that the object variant can't?
The text was updated successfully, but these errors were encountered: