arktypea
arktype2y ago
Micha

How to Union in scope()?

Union 1 will be resolved as I would expect to ts?: string | TimeStub | undefined;:

export const myType = type({
    "id?": "string",
    "coll?": "string",
    "ts?": ["string", "|", ["instanceof", TimeStub] as Infer<TimeStub>]
})


But Union 2

export const types = scope({
    account: {
        user: ["user", "|", ["instanceof", TimeStub] as Infer<TimeStub>],
        provider: "provider",
        providerUserId: "string"
    },
    user: {
        name: "string",
        "accounts?": "account[]"
    },
    provider: "'GitHub'|'Google'"
})

will be resolved to
account: { [k in keyof ({
        user: any;
        provider: "GitHub" | "Google";
        providerUserId: string;
    } & {} & {})]: ({
        user: any;
        provider: "GitHub" | "Google";
        providerUserId: string;
    } & {} & {})[k]; };

although I would expect something like user: User | TimeStub

If I'm trying to do the same with the base union syntax that I found
"ts?": "string|['instanceof', TimeStub] as Infer<TimeStub>"
I get a type error
Type '"string|['instanceof', TimeStub] as Infer<TimeStub>"' is not assignable to type '"string|string" | "string|number" | "string|bigint" | "string|boolean" | "string|symbol" | "string|undefined" | "string|object" | "string|null" | "string|Date" | "string|Error" | ... 25 more ... | "string|parsedDate"'.(2322)
demo.ts(44, 5): The expected type comes from property 'ts?' which is declared here on type '{ "id?": "string"; "coll?": "string"; "ts?": "string|string" | "string|number" | "string|bigint" | "string|boolean" | "string|symbol" | "string|undefined" | "string|object" | "string|null" | ... 27 more ... | "string|parsedDate"; }'


https://stackblitz.com/edit/rzkceh-eodur9?file=demo.ts
Was this page helpful?