Skip to content

Partial and indexed strings not reporting an error #20201

Closed
@dimitropoulos

Description

@dimitropoulos

TypeScript Version: 2.7.0-dev.201xxxxx (on the playground)
I tried to find a similar issue, but didn't see one.

Code
consider this to start

const myObject = {
    a: 'a',
    b: 'b',
    c: 'c',
};

interface MyType {
    a: string,
    b: string,
    c: string,
};

const updater = (update: Partial<MyType>) => ({
    ...myObject,
    ...update,
});

now let's say I want to make a subUpdater that is only responsible for handling a pre-specified set of the properties of MyType but not others.

const subUpdater = (updateProperty: 'a' | 'b', value: string) => {
    updater({
        [updateProperty]: value,
        d: 'd'
    })
}

rightly so, you get an error for d: 'd':

Argument of type '{ [x: string]: string; d: string; }' is not assignable to parameter of type 'Partial<MyType>'.
  Object literal may only specify known properties, and 'd' does not exist in type 'Partial<MyType>'.

however if you do:

/* notice the 'd' in the type of updateProperty */
const subUpdater = (updateProperty: 'a' | 'd', value: string) => {
    updater({
        [updateProperty]: value,
    })
}

it does not error.

Expected behavior:

the type of updateProperty should complain that the string is not compatible with Partial<MyType>

Actual behavior:

no error

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions