deepkit / deepkit-framework

A new full-featured and high-performance TypeScript framework
https://deepkit.io/
MIT License
3.2k stars 123 forks source link

type: intersections & unions don't work together #556

Closed fergusean closed 6 months ago

fergusean commented 7 months ago
test('intersections with unions', () => {
    type PortConfig = { protocol: 'tcp' | 'udp' } | { protocol: 'tls'; tlsCertPath: string; tlsKeyPath: string };
    type ServerConfig = PortConfig & {
        port: number;
    };

    const type = typeOf<ServerConfig>();
    const serialize = getSerializeFunction(type, serializer.deserializeRegistry);

    const test1 = serialize({ protocol: 'tcp', port: 123 });
    expect(test1).toEqual({ protocol: 'tcp', port: 123 });

    const test2 = serialize({ protocol: 'tls', tlsCertPath: 'a', tlsKeyPath: 'b', port: 456 });
    expect(test2).toEqual({ protocol: 'tls', tlsCertPath: 'a', tlsKeyPath: 'b', port: 456 });

    const test3 = serialize({ protocol: 'tls', port: 789 });
    expect(test3).toEqual({ protocol: 'tls', port: 789 });

    const test4 = serialize({ protocol: 'fake', port: 789 });
    expect(test4).toEqual({ port: 789 });

    // all 4 are "undefined"
});
marcj commented 6 months ago

@fergusean thanks for the report. this should be fixed in https://github.com/deepkit/deepkit-framework/commit/332b26eb148d916d03f49fad0daaad083c24207a. problem was that union& object literal intersection didn't expand each union member before, but that's now implemented

marcj commented 6 months ago

Fixed in https://github.com/deepkit/deepkit-framework/blob/master/CHANGELOG.md#101-alpha143-2024-03-17