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 does not work as described in README #45

Closed dyushin closed 10 months ago

dyushin commented 11 months ago

There is example in readme

function validateBody<T>(data: Assert<{ body: Resolve<T> }>) {
    return data.body;
}

const validatedBody = validateBody<{
    name: string,
    other: boolean
}>({ body: JSON.parse(process.argv[2]) });

// Transpiles to:
function validateBody(data) {
    return data.body;
}
const receivedBody = JSON.parse(process.argv[2]);
const validatedBody = (() => {
    const data = { body: receivedBody };
    if (typeof data.body !== "object" && data.body !== null)
        throw new Error("Expected data.body to be an object");
    if (typeof data.body.name !== "string")
        throw new Error("Expected data.body.name to be a string");
    if (typeof data.body.other !== "boolean")
        throw new Error("Expected data.body.other to be a boolean");
    return validateBody(data);
})();

I copied it to playground and instead of checking data.body i see null

function validateBody(data) {
    return data.body;
}
const validatedBody = (() => {
    const data = {
        body: JSON.parse(process.argv[2])
    };
    if (typeof data !== "object" || data === null)
        throw new Error("Expected data to be an object");
    if (typeof data.body !== "object" || data.body === null)
        throw new Error("Expected data.body to be an object");
    if (typeof null.name !== "string")
        throw new Error("Expected .name to be a string");
    if (typeof null.other !== "boolean")
        throw new Error("Expected .other to be a boolean");
    return validateBody(data);
})();
GoogleFeud commented 11 months ago

Thanks for the bug report!