Type X is not assignable to type Y (mapped type)

Viewed 22

I'm getting the Typescript error

Type '{ aaa: string; }' is not assignable to type '{ [P in keyof T]?: T[P]; }'.ts(2322)

related to the const c in the following code

class MyClass<T extends { aaa: string }> {
    public find(aaa: string) {
        const c: { [P in keyof T]?: T[P] } = { aaa };
    }
}

I've looked through many stack overflow posts that don't seem to help because I think this is particularly related to the mapped type.

I would expect that the mapped type defined above would let me use properties defined in the type constraint { aaa: string }.

Is that not true? Is there something about type contraints that aren't enumerable over mapped types?

Thanks in advance

0 Answers
Related