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

Problem with this type #27963

Closed
tao-cumplido opened this issue Oct 18, 2018 · 3 comments
Closed

Problem with this type #27963

tao-cumplido opened this issue Oct 18, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@tao-cumplido
Copy link
Contributor

TypeScript Version: 2.9.2, 3.1.1 (didn't try any other)

Search Terms:
this type
keyof this

Code

interface Foo {
    foo: number;
}

interface Bar {
    bar: number;
}

type FooBar = Partial<Foo & Bar>;
type FooBarMap<T extends FooBar> = { [P in Extract<keyof FooBar, keyof T>]: string };

declare function map<T extends FooBar>(o: T): FooBarMap<T>;

class A implements Foo {
    foo = 1;
    map = map(this);
    constructor() {
        console.log(this.map.foo) // unexpected: Property 'foo' does not exist on type 'FooBarMap<this>'.
        console.log(this.map.bar) // expected: Property 'bar' does not exist on type 'FooBarMap<this>'.
    }
}

class B implements Bar {
    bar = 1;
    map = map(this as B);
    constructor() {
        console.log(this.map.foo); // expected: Property 'foo' does not exist on type 'FooBarMap<B>'.
        console.log(this.map.bar);
    }
}

Expected behavior:
map(this) should be the same as map(this as A) for class A;

Actual behavior:
See code above or Playground.

I suspected the problem might be keyof this vs. keyof A, but I didn't manage to reduce the problem to it so I'm not sure.

Playground Link:
Link

Related Issues:
#24791

@ghost
Copy link

ghost commented Oct 18, 2018

I think this is the same problem as #27883. { [P in Extract<keyof FooBar, keyof T>]: string }; should contain at least "foo", but we don't have a way to represent that.

@ghost ghost added the Duplicate An existing issue was already created label Oct 18, 2018
@tao-cumplido
Copy link
Contributor Author

@andy-ms
I must admit, I don't think I fully grasp the issue you referenced, but I'm not sure they are related.

My problem here is that map(this) infers to map<this>(this) and it behaves differently than map<A>(this), i.e. when I use the explicit forms map<A>(this) or map(this as A) then it works as expected. But from my understanding the type this and the type A should behave the same, at least in the case above, or not?

@typescript-bot
Copy link
Collaborator

This issue has been marked as a duplicate and has seen no activity in the last day. It has been closed for automatic house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants