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

no mapping for this #12507

Closed
zpdDG4gta8XKpMCd opened this issue Nov 25, 2016 · 12 comments
Closed

no mapping for this #12507

zpdDG4gta8XKpMCd opened this issue Nov 25, 2016 · 12 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@zpdDG4gta8XKpMCd
Copy link

zpdDG4gta8XKpMCd commented Nov 25, 2016

i want to extend the Object prototype by adding a method that returns a proxified version of any object

Object.defineProperty(Object.prototype, 'proxify, {
   get: function(this: any) {
       // creating a proxy for the darn object
   }
});
const data = {
    name: 'hey',
    value: 123
};
const proxy = data.proxify();
declare global {
   interface Object {
     proxify(): {
        [P in keyof this]: Proxied<this [p]>; // expected to work, actual: A 'this' type is available only in a non-static member of a class or interface.
     }
   }
}

what am i doing wrong?

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Nov 26, 2016

What are the definitions of T and Proxied?

The following works but may not be what you are trying to accomplish

Object.defineProperty(Object.prototype, 'proxify', {
  get: function (this: any) {
      // creating a proxy for the darn object
  }
});
const data = {
    name: 'hey',
    value: 123
};
const proxy = data.proxify();
declare global {

  interface Object {
    proxify(): {
      [P in keyof this]: Proxied<this[P]>;
    };
  }
}
export type Proxied<T> = {
  [P in keyof T]: Proxied<T[P]>;
};

@aluanhaddad
Copy link
Contributor

For the sake of argument, isn't a proxy supposed to be transparent? 😛

@zpdDG4gta8XKpMCd
Copy link
Author

it doesnt really matter what Proxified is, because it breakes at this in [P in keyof this]

dont remember anythhing about them having to look exactly like the original objects (although it wont hurt if they do)

afaict, the only thing proxy should do to be standing between your object and you

@zpdDG4gta8XKpMCd
Copy link
Author

T should be read this, fixed

@aluanhaddad
Copy link
Contributor

I was being somewhat facetious. Seriously though it does appear that mapping this works correctly.

@zpdDG4gta8XKpMCd
Copy link
Author

haven't you seen 'this' type is available only in a non-static member of a class or interface anywhere?

@aluanhaddad
Copy link
Contributor

I've seen it a number of times but only in type aliases and other contexts where this would not be available.

declare global {
  interface Object {
    proxify(): {
      [P in keyof this]: Proxied<this[P]>;
    };
  }
}

Doesn't give me that error.

@zpdDG4gta8XKpMCd
Copy link
Author

say Proxified <T> = T what can you see wgen you type data.proxify ().? (whar does autocomplete suggest?)

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Nov 26, 2016

This is what I see
image
which is definitely not what we want here.

Update
It seems that mapping the type of this loses properties added via refinements and annotations. In order to have name and value expanded into the mapping I ended up with

  interface Object {
    proxify<T>(this: T): {
      [P in keyof T]: Proxied<T[P]>
    };
  }

Which works but obviously doesn't map this.

@HerringtonDarkholme
Copy link
Contributor

HerringtonDarkholme commented Nov 26, 2016

This is probably the same problem as #12349
and is not supported for now because of performance(I guess), reference #11929 (comment)

@aluanhaddad
Copy link
Contributor

@HerringtonDarkholme Thanks for providing the clarifying context, it is very helpful. I had totally forgotten that you had already raised an issue requesting this behavior, and I didn't realize the implications, or consider this the same issue.
In #12349, you state the specific problem concisely in the first sentence

it seems this type is resolved too eagerly in parent class.

I suggest we move any further discussion (back) into #12349.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 26, 2016

This is not the same as #12349. these are two different issues. the thing is internally, all objects get the apparent members of Object, but it is not that they extend object. there is no distinction if there are no this types, but it breaks if this types are defined on Object since these signatures or properties will not be instantiated with the type of the object you are using.
and yes, the concern is performance. since augmenting Object is not that common scenario.

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Nov 26, 2016
@mhegazy mhegazy closed this as completed Feb 28, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

4 participants