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

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

Closed
falsandtru opened this issue May 1, 2016 · 5 comments
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Suggestion An idea for TypeScript Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@falsandtru
Copy link
Contributor

TypeScript Version:

master

Code

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

Expected behavior:

error

Actual behavior:

pass

@mhegazy
Copy link
Contributor

mhegazy commented May 1, 2016

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.

@mhegazy
Copy link
Contributor

mhegazy commented May 1, 2016

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

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels May 1, 2016
@falsandtru
Copy link
Contributor Author

related: #8373

@DanielRosenwasser DanielRosenwasser added the Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript label May 2, 2016
@kitsonk
Copy link
Contributor

kitsonk commented May 2, 2016

I don't think they are actually related. It is being properly contextually typed as any. What I think @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.

@mhegazy mhegazy added Working as Intended The behavior described is the intended behavior; this is not a bug and removed In Discussion Not yet reached consensus labels May 16, 2016
@mhegazy
Copy link
Contributor

mhegazy commented May 16, 2016

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.

@mhegazy mhegazy closed this as completed May 16, 2016
@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
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Suggestion An idea for TypeScript Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants