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

Appearance settings

ProxyHandler no longer implementable in TS 2.4 #16933

Copy link
Copy link
@amirburbea

Description

@amirburbea
Issue body actions

TypeScript Version: 2.4.1

Code

I have a 3rd party component that requires occasionally to think it is receiving an object like an array but with keys as the string indexes of the window of the grid it represents and the values being the row objects. (IE: if I want to pass the grid rows 5-9 of my data source I need to send it

{ "5":{"id":5}, "6":{"id":6}, "7":{"id":7}, "8":{"id":8}, "9":{"id":9} }

I had a proxy handler that allowed me to pass in an array and a start index (so a 5 element array, and a start index of 5 in this example) and the proxy would be seen as this object by the grid.

But the code no longer compiles.

export class DataProxyHandler implements ProxyHandler<{}> {
  private static readonly descriptor: PropertyDescriptor = { configurable: true, enumerable: true, writable: false };

  constructor(public readonly startIndex: number, public readonly data: any[]) {
  }


  enumerate(target: {}) {
    return this.ownKeys(target);
  }

  get(target: {}, property: PropertyKey) {
    if (typeof property !== 'string') {
      return undefined;
    }
    const { startIndex, data } = this, index = parseInt(property);
    return isNaN(index) ? undefined : data[index - startIndex];
  }

  getOwnPropertyDescriptor(target: {}, p: PropertyKey) {
    return typeof p !== 'string' || !this.has(target, p) ? undefined : DataProxyHandler.descriptor;
  }

  getPrototypeOf(target: {}) {
    return Reflect.getPrototypeOf(target);
  }

  has(target: {}, property: PropertyKey) {
    if (typeof property !== 'string') {
      return false;
    }
    const { startIndex, data } = this, index = parseInt(property);
    return !isNaN(index) && index >= startIndex && index < startIndex + data.length;
  }

  isExtensible(target: {}) {
    return false;
  }

  ownKeys(target: {}) {
    const { startIndex, data } = this;
    return data.map((item, index) => (startIndex + index).toString());
  }

  set(target: {}, property: PropertyKey, value: any): boolean {
    throw new Error('Can not set');
  }
}

Expected behavior:

Code would compile

Actual behavior:

Types of property 'getOwnPropertyDescriptor' are incompatible.
Type '(target: {}, p: PropertyKey) => PropertyDescriptor | undefined' is not assignable to type '((target: {}, p: PropertyKey) => PropertyDescriptor) | undefined'.
Type '(target: {}, p: PropertyKey) => PropertyDescriptor | undefined' is not assignable to type '(target: {}, p: PropertyKey) => PropertyDescriptor'.
Type 'PropertyDescriptor | undefined' is not assignable to type 'PropertyDescriptor'.
Type 'undefined' is not assignable to type 'PropertyDescriptor'.

I can get it to compile by specifying the return type of the method getOwnPropertyDescriptor as any.

getOwnPropertyDescriptor(target: {}, p: PropertyKey): any {
    return typeof p !== 'string' || !this.has(target, p) ? undefined : DataProxyHandler.descriptor;
  }
Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    ExternalRelates to another program, environment, or user action which we cannot control.Relates to another program, environment, or user action which we cannot control.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.