Effect CommunityEC
Effect Community2y ago
7 replies
Gibbs

Troubleshooting TypeScript Function with Schema Type Constraints

hello there!
I'm trying to create a function that takes a generic schema in parameter and the passed schema should have a Type of a given shape:
const func = <T extends { id: string }>(schema: S.Schema<T>) => schema;

I can call this func with S.struct({ id: S.string }) without problem, as expected:
const res = func(S.struct({ id: S.string })); // compile 

But if the given schema has a branded string for the Id, then it does not compile:
const res2 = func(S.struct({ id: S.string.pipe(S.brand('Id')) })); // does not compile

The last line of the ts error is the following:
Type '{ readonly id: string; }' is not assignable to type '{ readonly id: string & Brand<"Id">; }'.

I was expecting that it would work since a string branded type extends string..
Was this page helpful?