-
-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to use a template literal type as a record()
key value
#291
Comments
import * as v from 'valibot';
const KeySchema = v.string([
v.custom((key) => {
const parts = key.split('-');
const num = parts.unshift();
if (Number.isNaN(Number(num))) return false;
const str = parts.join('-');
if (typeof str !== 'string') return false;
return true;
}),
]);
const FooSchema = v.record(KeySchema, v.string()); |
@fabian-hiller Sorry I have mistakenly post an old example. |
As I wrote import * as v from 'valibot';
type Key = `${string}-${number}`;
const KeySchema = v.special<Key>((key) => {
if (typeof key !== 'string') return false;
const parts = key.split('-');
const num = parts.unshift();
if (Number.isNaN(Number(num))) return false;
const str = parts.join('-');
if (typeof str !== 'string') return false;
return true;
});
// Not supported in <= v0.23.0
const FooSchema = v.record(KeySchema, v.string()); |
@fabian-hiller |
Yes, please leave it open. I will probably work on it later or tomorrow. |
Fixed. As soon as v0.24.0 is available, I will close this issue. Thank you for your contribution! |
@fabian-hiller |
v0.24.0 is available. |
I have tried to use the template literal type for a key of the record schema like this:
But the type error happened at the last line:
Here is the playground that tests this.
https://stackblitz.com/edit/typescript-c4dusa?file=index.ts
Is there proper a way to do this?
The text was updated successfully, but these errors were encountered: