Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

initial revision of external module augmentations#6213

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vladima merged 10 commits intomasterfrommoduleAugmentations
Jan 16, 2016

Conversation

@vladima
Copy link
Contributor

Module augmentation is a declaration of ambient module that directly nested either in external module or in top level ambient external module.
Name of module augmentation is resolved using the same set of rules as module specifiers in import \ export declarations.
If name is successfully resolved to some external module then declarations in module augmentation are merged with declarations inside module using standard rules for declaration merging.
Module augmentations cannot add new items to the top level scope but rather patch existing declarations.

for example

// observable.tsexportclassObservable<T>{}
// map.tsimport{Observable}from"./observable";Observable.prototype.map=function(){/* ... */}declare module"./observable"{interfaceObservable<T>{map<U>(proj:(el:T)=>U):Observable<U>;}}
// consumer.tsimport {Observable} from"./observable";import"./map";let o: Observable<number>;o.map(x=> x.toFixed());

Here modulemap can declare that internally it will patchObservable type and addmap function to it.

Fixes:#5269,#6022

Pending work:

  • - add syntactic support for augmenting global scope. Currently augmentation for the global scope is defined as ambient module with special name"/" but this is just a placeholder. The version that we ended up during the design meeting was:
// in external moduleexport{};declare global{}

or

// in script filedeclare module"array"{global{}}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't see this getting used anywhere apart from the later assignment.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

right, forgot to remove that. Thanks

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

isGlobalAugmentation or augmentsGlobal

@DanielRosenwasser
Copy link
Member

What exactly is the recommended way to augment a default export? Is it possible in this implementation?

@vladima
Copy link
ContributorAuthor

it is not possible

@DanielRosenwasser
Copy link
Member

This also appears to take care of#2784. You might want to take note and give a heads up on that issue when this goes in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

what about external modules with no exports or no module augmentation? soemthing like:

import{a}from"./mod";// do something with a

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

i thought we will do this for any external module, that we never emitted any import, export, or module augmentation for.

@mhegazy
Copy link
Contributor

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This should becheckModuleAugmentationElement. It checks and element in the body, not the body itself.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@vladima
Copy link
ContributorAuthor

build failure is related to#6478

@vladima
Copy link
ContributorAuthor

@ahejlsberg done, do you have any other comments?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The error message below seems a bit odd for this case, but maybe it's fine.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

for global case we can easily lift this restriction since it is possible to add new entries to global scope from within a module. Currently the fact that we have it is only because of consistency in behavior for augmentations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

No sure I understand, maybe we're talking about different issues. This code is saying that you get an error when you have an exported member in a global augmentation block, right? I'm thinking it should never be valid to useexport in a global augmentation block.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

the idea of this check is following:symbol.parent will beundefined if symbol was initially defined in the global scope. Ifsymbol.parent is notundefined this means that this symbol was declared inside augmentation and will declare new entry in the global scope which is disallowed in current design

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

If symbol.parent is not undefined this means that this symbol was declared inside augmentation

Why is that so?symbol.parent is non-undefined when the symbol is exported, but it might still be declared inside the augmentation. I'm not getting this.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

symbols whose initial declaration is in global scope are never exported andsymbol.parent for them is alwaysundefined. ifsymbol.parent !== undefined this means that this symbol is exported from somewhere and as a consequence initially defined in augmentation not in global scope

@masaeedu
Copy link
Contributor

Inmap.ts: since the ambient module declaration advertises to the compiler thatObservable<T> has amap member, would you be able to attach a function of the appropriate kind to the prototype without casting to<any>?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I have a hard time reasoning about this.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

this assert is redundant. it basically says: augmentation can only change exported symbol (parent !== undefeind) that really has a declaration (not a virtual symbol likeprototype) - this should always be the case. I'll remove it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

So did you meansymbol.valueDeclaration !== undefined? You currently havesymbol.parent.valueDeclaration.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

symbol.parent.valueDeclaration - is an augmentation:declare module ....symbol.valueDeclaration is declaration inside the augmentation. Currently we report error if container that this symbol is module augmentation and not a normal external module

vladima added a commit that referenced this pull requestJan 16, 2016
initial revision of external module augmentations
@vladimavladima merged commitc6b0cf1 intomasterJan 16, 2016
@vladimavladima deleted the moduleAugmentations branchJanuary 16, 2016 05:28
@benlesh
Copy link

Awesome! Great stuff. Thank you!

@DanielRosenwasser
Copy link
Member

🎉 👏 👏 👏 👏 👏 👏 👏

@weswigham
Copy link
Member

👍

Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Type generation for "modularly designed" libraries is hard

10 participants

@vladima@DanielRosenwasser@mhegazy@masaeedu@benlesh@weswigham@mihailik@ahejlsberg@msftclas

[8]ページ先頭

©2009-2025 Movatter.jp