镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

noImplicitAny checks don't work for a setter parameter of Object.defineProperty #8410

Description

@falsandtru

TypeScript Version:

master

Code

Object.defineProperty({}, '', {
  set(a) { // a is an implicit any type
  }
});

Expected behavior:

error

Actual behavior:

pass

Activity

  1. mhegazy commented on May 1, 2016

    @mhegazy
    Contributor

    it gets a contextual type from the declaration, see https://github.com/Microsoft/TypeScript/blob/master/lib/lib.d.ts#L81.

    we could change the definition of defineProperty to use TypedPropertyDescriptor instead of PropertyDescriptor, that will make your setter argument inferred from declaration, so it will be {} and not any, which would be closer to an error. but that would be a breaking change.

  2. mhegazy commented on May 1, 2016

    @mhegazy
    Contributor

    so just to be clear, the reported behavior is expected given the lib.d.ts definition. if there is anything to do is to change the definition in lib.d.ts

  3. falsandtru commented on May 1, 2016

    @falsandtru
    ContributorAuthor

    related: #8373

  4. kitsonk commented on May 2, 2016

    @kitsonk
    Contributor

    I don't think they are actually related. It is being properly contextually typed as any. What I think Mohamed Hegazy (@mhegazy) is suggesting is that it might be worth this being implemented in lib.d.ts in the future, as it stands at the moment it will always be non-implicit any because of the contextual typing:

    interface PropertyDescriptor<T> {
        configurable?: boolean;
        enumerable?: boolean;
        value?: T;
        writable?: boolean;
        get? (): T;
        set? (v: T): void;
    }
    
    interface ObjectConstructor {
        defineProperty<T>(o: any, p: string, attributes: PropertyDescriptor<T>): any;
    }

    I suspect there are a few other places too that would need to be adjusted.

  5. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    and removed on May 16, 2016
  6. mhegazy commented on May 16, 2016

    @mhegazy
    Contributor

    As noted earlier, there is a context type passed through, and that suppresses the noImplicitAny errors. you can get a better behavior by defining an extra overload of define property as:

    interface ObjectConstructor {
        defineProperty<T>(o: any, p: string, attributes: TypedPropertyDescriptor<T>): any;
    }

    it is not clear there is much value in changing the lib.d.ts definitions for everybody though.

  7. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptSuggestionAn idea for TypeScriptWorking as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions