Skip to content

Unable to overload arrow function in module augmentation #39622

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
dotansimha opened this issue Jul 16, 2020 · 4 comments
Closed

Unable to overload arrow function in module augmentation #39622

dotansimha opened this issue Jul 16, 2020 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@dotansimha
Copy link

I'm trying to overload an existing arrow function in a module, exported as const. It works perfectly fine with overrides for function, class and interface, but failing for arrow function declared as const.

Also tried with:

    export interface myVar {
        (param: number): number
    }

It works, but then the arrow function become unusable because it treats it as interface and it's not callable.

TypeScript Version: 3.9.2

Search Terms: const, module augmentation, declaration merging, overload, block-scope variable

Expected behavior:

Allow to override block-scoped variables if it's referring to an arrow function. Just like a function.

Actual behavior:

Cannot redeclare block-scoped variable 'myVar'.(2451)

Related Issues:

Code

declare module 'test' {
    // export function works
    export function myFunc(param: string): string;

    // export const doesn't work
    export const myVar: (param: string) => string;
}

declare module 'test' {
    export function myFunc(param: number): number;
    export const myVar: (param: number) => number;
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 17, 2020
@RyanCavanaugh
Copy link
Member

This is the intended behavior; consts are assumed to be in conflict but multiple function declarations adds new overloads.

@dotansimha
Copy link
Author

dotansimha commented Jul 19, 2020

Thanks @RyanCavanaugh !
Maybe I'm missing something here, but const that point to an arrow function is basically a function...

@orta
Copy link
Contributor

orta commented Jul 20, 2020

The goal of that message (I assume) is to match runtime behavior where the function declaration acts like var vs the const which never allow overriding:

❯ node
Welcome to Node.js v13.6.0.
Type ".help" for more information.
> function a() {}
undefined
> function a() {}
undefined

> const b = () => {}
undefined
> const b = () => {}
Uncaught SyntaxError: Identifier 'b' has already been declared

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants