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

Conflict betwen Partial and 'this' type #13259

Closed
WanderWang opened this issue Jan 3, 2017 · 4 comments
Closed

Conflict betwen Partial and 'this' type #13259

WanderWang opened this issue Jan 3, 2017 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@WanderWang
Copy link

TypeScript Version: 2.1.4 / nightly (2.2.0-dev.20170102)

Code

class Tween<T>{

    static get<T>(target: T): Tween<T> {
        return new Tween(target);
    }

    constructor(private target: T) {
    }

    public to(props: Partial<T>) {
    }
}

class DisplayObject {
    public x: number;
    public y: number;
}


class Button extends DisplayObject {

    public z: number;
    onClick = () => {
        let tween = Tween.get(this);
        tween.to({ x: 100 });
        tween.to({ z: 100 });
        tween.to({ a: 100 });
    }
}

Expected behavior:

        let tween = Tween.get(this); 
        tween.to({ x: 100 }); // right 
        tween.to({ z: 100 }); // right 
        tween.to({ a: 100 }); // compile error

Actual behavior:

        let tween = Tween.get(this); 
        tween.to({ x: 100 }); // compile error 
        tween.to({ z: 100 }); // compile error 
        tween.to({ a: 100 }); // compile error
@dsehnal
Copy link

dsehnal commented Jan 3, 2017

I have observed a possibly related issue in nightly (2.2.0-dev.20170103) with the code:

export abstract class A<P> { public props: P; }

export abstract class B<T, P extends { params: T }> extends A<P> {
    public get params(): this['props']['params'] { return this.props.params; }
    update(params: Partial<this['params']>) {
        // ...
    }
}

export interface P { x: number, y: string }
export class C extends B<P, { params: P }> {
    f() {
        const p = this.params;
        this.update({ x: p.x + 1 }); // compile error here
    }
}

@WanderWang
Copy link
Author

Who can give me a feedback ?

@ahejlsberg @DanielRosenwasser

@ahejlsberg
Copy link
Member

@WanderWang I'd suggest changing the to method to use Pick<T, K>:

    public to<K extends keyof T>(props: Pick<T, K>) {
    }

For more discussion on a similar issue, see #12793.

@WanderWang
Copy link
Author

thanks @ahejlsberg , it works !!! I think I can close this issue and concern about #12793

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jan 10, 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
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants