Skip to content

Two types should only be compatible if their members are compatible - even if all these members are optional #12904

Description

I wanted to see if I can update definitions a MongoDB filter object, which utilizes these expressions. Below is just a start, but I already see an issue when the code compiles when it should not. This issue possibly has similar underlying issue as #12769

TypeScript Version: 2.1.4

Code

typeMongoFilter<T>=$and<T>|$or<T>|propertyRule<T>;typelogical<T>=$and<T>|$or<T>;type$and<T>={$and: Array<logical<T>|propertyRule<T>>}type$or<T>={$or: Array<logical<T>|propertyRule<T>>}typepropertyRule<T>={[PinkeyofT]?: T[P]|comparison<T[P]>}typecomparison<V>=$eq<V>|$gt|$lt;type$eq<V>={$eq: V};type$gt={$gt: number};type$lt={$lt: number};// See if MongoFilter works:interfacePerson{id: number;name: string;age: number;}// should compileletf1: MongoFilter<Person>={$or: [{id: 1},{name: 'dima'}]};letf2: MongoFilter<Person>={$and: [{id: 1}]};letf3: MongoFilter<Person>={id: 1};// should not compileletw1: MongoFilter<Person>={wrong: 1};// does not compile - goodletw2: MongoFilter<Person>={$or: [{wrong: 1}]};// compiles

Expected behavior:
Code below should not compile

letw1: MongoFilter<Person>={$or: [{wrong: 1}]};

Actual behavior:
But it does

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions