TypeScript Version: 4.1.2 and 4.2.0-dev.20201123
Search Terms:UpdateQuery, Intersection, Generic
Code
import{UpdateQuery}from'mongodb';interfaceFoo{foo: string;}functiontestWithGeneric<TSchema>(): UpdateQuery<TSchema>{constupdate: UpdateQuery<TSchema>={};if(Math.random()<0.5){constkey: string='newKey';constvalue: any=true;update.$set={
...(update.$set||{}),[key]: value};}returnupdate;}functiontestWithoutGeneric(): UpdateQuery<Foo>{constupdate: UpdateQuery<Foo>={};if(Math.random()<0.5){constkey: string='newKey';constvalue: any=true;update.$set={
...(update.$set||{}),[key]: value};}returnupdate;}Where the UpdateQuery definition in the mongodb types is defined as:
typeDotAndArrayNotation<AssignableType>={readonly[key: string]: AssignableType;};typeReadonlyPartial<TSchema>={readonly[keyinkeyofTSchema]?: TSchema[key];};exporttypeMatchKeysAndValues<TSchema>=ReadonlyPartial<TSchema>&DotAndArrayNotation<any>;exporttypeUpdateQuery<TSchema>={$set?: MatchKeysAndValues<TSchema>;// other fields here don't matter};Expected behavior:
Code is valid.
Actual behavior:
Only the function testWithGeneric throws an error. But since MatchKeysAndValues is defined as an intersection between ReadonlyPartial<TSchema> and a mapped type of string keys with any values, it should be accepted.
Type '{ [x: string]: any; }' is not assignable to type 'MatchKeysAndValues<TSchema>'. Type '{ [x: string]: any; }' is not assignable to type 'ReadonlyPartial<TSchema>'.
This used to work in version 4.0.5.
Playground Link:https://www.typescriptlang.org/play?ts=4.1.2#code/JYWwDg9gTgLgBAbzgVTAEwIYwKYEUCu2UAnnAL5wBmUEIcA5CBAHYDmEaARvQNwCwAKEHBmOKJQwBjbHABiECIkFwqCgFxwAzjCgjW-AWUGDK+ZpJjAWcHNoDqwGAAsA4tmZFgkgDwAVAMqSTtggGAB8ABQAlBqomDgERMR+gcGhYUoCKpIs2nD46FjYsYUJhCQpQSHhcAC8iGQGynDAlHARALJYTgB0UBjMaLTRcN5wAAw9AKxRmSrZufAA1tjEGtq6bHUMHgDuANKrvM0LzHkAbhgANoQaA6T1OoRNWfMF8dg9ACSa2PD1CBO8x6IIi7yK31+8AAPtCGlEADRAlQAbRWxAAuhpLjdsEDGs0jEJXlA-vgoMx8qVsAYiSYzBYrJTbDAHM4IPgYG4PLpJNESh9EhV5BAMoDXjkzvBwTgBUUhckRRkAQTiSpWu0us4+gMhiARmNJjM5vNJXl0esdHptvQ9odiMdXqcLtdbnB7tsnjTjE6qR9IX9tuL5sDQTLPj9A7D4Ujfaj0Vi4DjCPiDCo6SSyRS-UVaYIgA
TypeScript Version: 4.1.2 and 4.2.0-dev.20201123
Search Terms:
UpdateQuery,Intersection,GenericCode
Where the
UpdateQuerydefinition in themongodbtypes is defined as:Expected behavior:
Code is valid.
Actual behavior:
Only the function
testWithGenericthrows an error. But sinceMatchKeysAndValuesis defined as an intersection betweenReadonlyPartial<TSchema>and a mapped type of string keys with any values, it should be accepted.This used to work in version 4.0.5.
Playground Link:https://www.typescriptlang.org/play?ts=4.1.2#code/JYWwDg9gTgLgBAbzgVTAEwIYwKYEUCu2UAnnAL5wBmUEIcA5CBAHYDmEaARvQNwCwAKEHBmOKJQwBjbHABiECIkFwqCgFxwAzjCgjW-AWUGDK+ZpJjAWcHNoDqwGAAsA4tmZFgkgDwAVAMqSTtggGAB8ABQAlBqomDgERMR+gcGhYUoCKpIs2nD46FjYsYUJhCQpQSHhcAC8iGQGynDAlHARALJYTgB0UBjMaLTRcN5wAAw9AKxRmSrZufAA1tjEGtq6bHUMHgDuANKrvM0LzHkAbhgANoQaA6T1OoRNWfMF8dg9ACSa2PD1CBO8x6IIi7yK31+8AAPtCGlEADRAlQAbRWxAAuhpLjdsEDGs0jEJXlA-vgoMx8qVsAYiSYzBYrJTbDAHM4IPgYG4PLpJNESh9EhV5BAMoDXjkzvBwTgBUUhckRRkAQTiSpWu0us4+gMhiARmNJjM5vNJXl0esdHptvQ9odiMdXqcLtdbnB7tsnjTjE6qR9IX9tuL5sDQTLPj9A7D4Ujfaj0Vi4DjCPiDCo6SSyRS-UVaYIgA