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

Partial and indexed strings not reporting an error #20201

Closed
dimitropoulos opened this issue Nov 21, 2017 · 4 comments
Closed

Partial and indexed strings not reporting an error #20201

dimitropoulos opened this issue Nov 21, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@dimitropoulos
Copy link
Contributor

dimitropoulos commented Nov 21, 2017

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

@mhegazy
Copy link
Contributor

mhegazy commented Nov 21, 2017

The type of { [updateProperty]: value } should be { "a" : string } | { "d": string } that is currently not the case, and is tracked by #13948. once that is fixed, you should get the error as expected.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Nov 21, 2017
@dimitropoulos
Copy link
Contributor Author

good point, in my case I made the minimal case a little too general.

interface MyType {
    a: string,
    b: number,
    c: string[],
};

if MyType looks like this (which is closer to my actual use-case), your suggestion doesn't work exactly.

thanks for that other issue, I'll take a gander and see if I can perhaps help there.

@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.

@sandersn
Copy link
Member

sandersn commented Jan 8, 2018

I tested the fix #21070 and it indeed gives an error for both definitions of MyType.

@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

4 participants