Skip to content
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

Declaration merging #28

Open
aspirisen opened this issue May 18, 2023 · 4 comments
Open

Declaration merging #28

aspirisen opened this issue May 18, 2023 · 4 comments

Comments

@aspirisen
Copy link

Hello, will it be possible to merge declarations, like that:

module Hello {
    export const A = 3;
}

module Hello {
    export const B = 4;
}

console.log(Hello.A, Hello.B) // 3, 4

This is how namespaces works in typescript (previously they called modules). Playground

One huge benefit of namespaces is that they can be merge with functions declarations. Playground

function Hello() {
    return 3
}

namespace Hello {
    export const meta = 4
}

console.log(Hello(), Hello.meta) // 3, 4

This is a widely used pattern i.e. in react. Playground

export function Component(props: PropsWithChildren) {
    return <div>{props.children}</div>
}

export namespace Component {
    export function Icon() {
        return <i className="icon" />
    }
}
@ljharb
Copy link
Member

ljharb commented May 18, 2023

That's the major reason I avoid namespaces in TS - the Ruby-esque ability to reopen and edit a definition in my experience makes code much harder to reason about.

If you want to compose modules, imo you should use the current tools and import + re-export, rather than smooshing the code together.

@aspirisen
Copy link
Author

@ljharb merging modules is just one of the possibilities, anyway they will be in one file. Actually, I've never used namespaces merging in TypeScript.

But merging namespace with function is pretty common use case to add logically related things to the function. Probably this will be the target use case for declaration merging. Yes, you will be able to merge modules by that way, but I can hardly imagine why one will need it since everything is in the same file.

@Vasile-Peste
Copy link

Vasile-Peste commented May 29, 2023

That's the major reason I avoid namespaces in TS - the Ruby-esque ability to reopen and edit a definition in my experience makes code much harder to reason about.

If you want to compose modules, imo you should use the current tools and import + re-export, rather than smooshing the code together.

What if we optionally declare if a module can be merged? Something like partial classes in C#. This makes the reader aware that some members of the module might come from other merged declarations.

// ok
partial module A {
    export const foo = ...;
}


// ok
partial module A {
    export const bar = ...;
}
// ok
module A {
    export const foo = ...;
}

// Redeclaration error
module A {
    export const bar = ...;
}

Reference
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods

@littledan
Copy link
Member

This proposal does not permit merging. The code sample at the beginning of the thread would cause an error at parse time. (However, this is not meant to pass judgement on TS declaration merging, which I don’t know if the world could live without!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants