Skip to content

Adding keyword-named properties to functions generates invalid declarations #38750

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
ark120202 opened this issue May 24, 2020 · 1 comment · Fixed by #38982
Closed

Adding keyword-named properties to functions generates invalid declarations #38750

ark120202 opened this issue May 24, 2020 · 1 comment · Fixed by #38982
Assignees
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this

Comments

@ark120202
Copy link

TypeScript Version: 4.0.0-dev.20200522

Search Terms:

Code

function foo() {}
foo.null = true;

Expected behavior:

Any syntactically valid output. Since namespaces can't hold keyword names one option is generating an object type with call signature:

declare var foo: {
    (): void;
    null: boolean;
}

This change would also allow to generate declarations for statically-analyzable dynamic property access expressions (i.e. foo["foo bar"] = true), which are currently omitted from declarations, resulting different behavior between in-project and out-of-project values.

Actual behavior:

declare function foo(): void;
declare namespace foo {
    var null: boolean;
    //        ^^^^^^^ TS2693: 'boolean' only refers to a type, but is being used as a value here.
    //  ^^^^^ TS1134: Variable declaration expected.
}

Playground Link: https://www.typescriptlang.org/play?ts=4.0.0-dev.20200522#code/GYVwdgxgLglg9mABMOcAUBKRBvAvgKBTgDowQAbcxAXkSgCcQBTAbiA

Related Issues:

@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this labels May 26, 2020
@DanielRosenwasser DanielRosenwasser added this to the Typescript 4.0.1 milestone May 26, 2020
@DanielRosenwasser
Copy link
Member

We can generate a temp name and re-export with the right name.

declare function foo(): void;
declare namespace foo {
    var _null: boolean;
    export { _null as null };
}

Just make sure that everything else is still exported (i.e. yadda is accessible in the generated .d.ts file for the following).

function foo() {}
foo.yadda = 100;
foo.null = true;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this
Projects
None yet
3 participants