Comments (3)
@Qrokqt Hi, try the following...
import { Type, TObject, TString } from '@sinclair/typebox'
type Allowed = TObject<{ [key: string]: TString }>
function doSomething<T extends Allowed>(schema: T) { /* ... */ }
//good
doSomething(Type.Object({}));
doSomething(Type.Object({
foo: Type.String(),
bar: Type.String(),
}));
//bad
doSomething(Type.Number());
doSomething(Type.Object({
foo: Type.Number(),
});
doSomething(Type.Object({
foo: Type.Object({
bar: Type.String(),
}),
}));
Hope this helps
S
from typebox.
Perfect, thanks!
from typebox.
Unfortunately this is not a general solution. I'm trying to define a SchemaFor<T>
but even the simplest attempt runs into a "type instantiation is excessively deep and possibly infinite" error.
This is what I tried:
type SchemaFor<T> = T extends string ? ReturnType<typeof Type.String>
: T extends number ? ReturnType<typeof Type.Number>
: T extends boolean ? ReturnType<typeof Type.Boolean>
: T extends object ? TObject<{ [P in keyof T]: SchemaFor<T[P]> }>
: TSchema;
from typebox.
Related Issues (20)
- Custom paths for custom validation errors HOT 2
- Composite Union type does not work HOT 3
- Type.Recursive does not work with Type.Transform for StaticDecode
- Issue regarding nested objects when using Value.Default HOT 5
- Enum type inferred as never when using Type.Mapped() HOT 2
- Should `IsValueType` guard consider `Date`? HOT 1
- ESNext target HOT 3
- Trying to do codegen from Drizzle schemas HOT 1
- Make `default` and `examples` schema options properties type-safe HOT 2
- Indicate which files don't have "sideEffects" in package.json for improved tree shaking HOT 3
- Type.String().Optional() is accepted on ts-hint / ts-lint, and even build successfully; but of course, "Optional()" does not exists HOT 2
- Dynamic Template Literals can't be Mapped HOT 2
- How should TypeBox types be used with TS type predicates? HOT 2
- TypeCompiler.Compile incorrectly parsing `Record<any, any>`
- Error when using Composite with Ref (Unable to dereference schema with $id 'undefined') HOT 3
- Transform type keys in Record
- Is it possible to add support to the Type.String additional properties for the enum keyword HOT 2
- Support for async validations?
- Transform + Map/Index does not work HOT 2
- Update the error message for required properties
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typebox.