-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Variable merging #4062
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
What's the intent of this code? |
First declaration is from nuget package. I do not want to modify it. |
We've had this request before. The problem is how do we support this dynamic, expando type of |
It would be nice if it will work the same way as in the case of interface merging. I.e. interpret a type of the variable, as a declaration of an anonymous interface: declare interface _anonymous_interface_for_global_var_test: {
(s: string) => void;
}
declare interface _anonymous_interface_for_global_var_test: {
(s: string) => void;
tests: any[];
} |
If we allow this, it seems to imply we should allow extension of existing union types. But maybe I am wrong. |
It might be useful to support this in the particular case of a property of type // DefinitelyTyped
interface I { foo: any; }
// My code
interface I { foo: { x: number }; } Currently we give an error because the types for |
Just ran into this again trying to define the type of |
history
: use interface for LocationState
to allow augmentation
DefinitelyTyped/DefinitelyTyped#41568
I have a "header" type of interface:
I want to have extended interface:
if there is no inex type in
It will allow for elegant and typesafe solution. We can easily mimic this behavior with
but this results in unnecessary JS code emitted and ran |
Here is another example. Let's say we're making a library of utility functions. We want to import some of the utility functions into a front-end project. To keep things organized, we want the utility functions to always be namespaced. For example, we want to require our developers to call (The inspiration is the popular library Rxjs, which exports functions with very generic names like Simplest but worst solutionThe library could have an index file that exports all the helper functions, which can then be imported with import * as utils from `utils/index.ts`;
utils.foo();
utils.bar(); However, without some very aggressive tree-shaking this would add all the helper functions into the project's Javascript bundle, which would be a lot of unnecessary bloat if the project only actually uses a few of the functions. Mediocre solutionThe project could have a file that is an interface between the project and the library, in which we import and export just the functions we want to use in the project: // src/utils.ts
export { foo } from 'utils/foo.ts';
export { bar } from 'utils/bar.ts'; // src/app.ts
import * as utils from 'src/utils.ts';
utils.foo();
utils.bar(); However, this means someone has to maintain that interface, and if the project grows to encompass many modules with separate JS bundles, we could run into the same bloat problem as before. Other mediocre solutionWe could try to enforce a style rule that the function names have to be prefixed: // src/app.ts
import { foo as utilsFoo } from 'utils/foo.ts';
import { bar as utilsBar } from 'utils/bar.ts'; ...but that's annoying boilerplate to write, and is hard to enforce. What we'd likeWe'd like to (a) import just the modules we want, while (b) preserving namespacing. We can declare additional global properties by adding them to the declare global {
interface HTMLElementEventMap {
myCustomEvent: CustomEvent;
}
} It would be nice if there was a way to do something similar with other objects. To my mind it would look something like this: // util/index.ts
export const utils = {}; // util/foo.ts
function foo() {};
utils.foo = foo;
declare module utils {
export const foo = typeof foo;
} // util/bar.ts
function bar() {};
utils.bar = bar;
declare module utils {
export const bar = typeof bar;
} // src/app.ts
import { utils } from 'util/index.ts';
import 'util/foo.ts'; // This causes `utils.foo` to be defined
utils.foo();
utils.bar(); // Error: undefined |
At the moment TS compiler shows error:
Subsequent variable declarations must have the same type.
The text was updated successfully, but these errors were encountered: