Skip to content

Function allows me to build invalid partial #55672

Closed as not planned
Closed as not planned
@f-g-p

Description

@f-g-p

Hello,

I have the following piece of code that allows me to produce an invalid partial object:

type A = { a: number; b: string };

const buildPartial = (key: keyof A, val: A[keyof A]): Partial<A> => ({ [key]: val });
const test = buildPartial('a', 'hello'); // allows me to build an invalid object without compile error

Shouldn't I have a compile error? { [key]: val } seems to of type { a: number | string } | { b: number | string } which is not compatible with Partial
Thanks
ps: I understand I should have written const buildPartial = <K extends keyof A>(key: K, val: A[K]) => ... My question is not on this, it is on why this was not caught by the compiler

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