GoogleFeud / ts-runtime-checks

A typescript transformer that automatically generates validation code from your types.
https://googlefeud.github.io/ts-runtime-checks/
MIT License
312 stars 7 forks source link

[BUG] Resolve strange behavior #46

Closed dyushin closed 10 months ago

dyushin commented 11 months ago

Let's look at this code fragment from playground

const f = <T>(val: Assert<Resolve<T>>) => {
    return val
}

console.log(f<{ name: boolean }>({ value: { name: "Ivan" } } as any))
console.log(f<{ name: number }>({ value: { name: true } } as any))

after tranpilation we get this code

const f = (val) => {
    return val;
};
console.log((() => {
    const val = { value: { name: "Ivan" } };
    if (typeof val !== "object" || val === null)
        throw new Error("Expected val to be an object");
    if (typeof null.name !== "boolean")
        throw new Error("Expected .name to be a boolean");
    return f(val);
})());
console.log((() => {
    const val = { value: { name: true } };
    if (typeof val !== "object" || val === null)
        throw new Error("Expected val to be an object");
    if (typeof null.name !== "boolean")
        throw new Error("Expected .name to be a boolean");
    return f(val);
})());

There are 2 problems: 1) null.name instead of val.name but i have already created issue with this bug 2) in both console.log we see "Expected .name to be a boolean", but name in second function expected to be a number, not boolean. It seems that the compiler only considers the first function call and copies it to the others

GoogleFeud commented 11 months ago

Thanks for the bug report, will be fixed very soon!

dyushin commented 10 months ago

Hi! Thanks for the quick fixes Can I ask when the package is scheduled for release?

GoogleFeud commented 10 months ago

Hi, I've been quite busy, a new version will be released before Christmas for sure!