woutervh- / typescript-is

MIT License
956 stars 35 forks source link

Intersection disables `disallowSuperfluousProperties` all the way down #111

Closed lukas-valenta closed 2 years ago

lukas-valenta commented 3 years ago

With disallowSuperfluousProperties on, the following code results in wrong typecheck:

        type Foo = {
                quux: string;
            };

        type BarIntersection = {
            foo: Foo;
        } & {
            bar: string;
        };

        type BarNotIntersection = {
            foo: Foo;
            bar: string;
        };

        const document = {
            bar: 'bar',
            foo: {
                quux: 'quux',
                super: 'fluous'
            }
        };

The super: 'fluous' property does not get caught with is<BarIntersection>, but is<Foo> and is<BarNotIntersection> both catch it. This is because typescript-is disables disallowSuperflousProperties when encountering an intersection (and for a good reason), but never restores it. I have written a test case and a quick-and-dirty fix, I will post a PR soon.

NOTE: This is just minimal reproduction, obviously the intersection is not needed in this example.