Skip to content

Partial<T> types not working as expected, when extending another interface #21196

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

Closed
jamespacileo opened this issue Jan 16, 2018 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@jamespacileo
Copy link

TypeScript Version: 2.7.0-insiders.20180108

Code

interface Vehicle{
    wheels: number;
}

class Driver<T extends Vehicle>{
    car: Partial<T>;
    constructor(){
        this.car = {wheels: 4} 
        // Type '{ wheels: 4; }' is not assignable to type 'Partial<T>'.
        // (property) Driver<T extends Vehicle>.car: Partial<T>
    }
}
@jcalz
Copy link
Contributor

jcalz commented Jan 16, 2018

So, this is one of many duplicates of #13442, and I imagine that this will be closed.

Someone may break out an example like:

interface TractorTrailer extends Vehicle {
  wheels: 18;
  cargo: string;
}

const theBandit = new Driver<TractorTrailer>();
theBandit.car.wheels // 4 at runtime, 18 at compile time... oops

But while we're here, could someone explain why this doesn't work?

function explainPlease<T extends Vehicle>() {
  const err1: Partial<T> = null! as { wheels: T['wheels'] }; // error
  const err2: Partial<T> = { wheels: void 0 }; // error
}

If I inspect Partial<T> I see that it has a property wheels of type T['wheels'] | undefined, and all properties are optional, so I'd expect the above to compile without warning. Am I missing something, or is it just that the compiler doesn't actually try to check whether something is assignable to Partial<T>?

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jan 17, 2018
@Pmyl
Copy link

Pmyl commented Jan 24, 2018

I can see that some examples seems completely safe, look at this and please tell me if I'm wrong:

interface Entity {
    id: string;
}

function aFunction<TItem extends Entity>(): void {
    const partialItem: Partial<TItem> = { id: '0' };
    // Type { id: "0"; } is not assignable to type Partial<TItem>
}

function anotherFunction<TItem extends Entity>(item: TItem): void {
    const partialItem: Partial<TItem> = item; // this works correctly
    item.id = "new";
    partialItem.id = "new"; // this works correctly
}

I know that TItem extends Entity (pseudo code)
TItem extends Entity => { id: string; ...: any; }
and with Partial everything becomes optional (pseudo code)
Partial<TItem> => { id?: string; ...?: any; }
so the "id" property should be there and everything else can be omitted (like I did), why the compiler is telling me that it's not assignable?

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants