Comments (4)
typeof user.TYPE
from superstruct.
import { number, object, optional, string } from "superstruct";
const user = object({
id: number(),
name: string(),
some: optional(string()),
});
console.log(user.schema.name.validate(undefined)[0] == null) // false
console.log(user.schema.some.validate(undefined)[0] == null) // true
from superstruct.
This task was not very difficult. By writing a few helper functions, I was able to get the output I wanted. However, there is a serious problem. In Runtime, I did not find anything related to a field being optional. Actually, I have written a framework called Lesan with Superstruct
. You can see the code I wrote to create the types here:
https://github.com/MiaadTeam/lesan/blob/main/src/types/generateTypesFromStruct.ts
Of course, this code has not been fully written yet.
But the problem is when we create an object with the following two fields:
object({
optionalName: optional(string()),
name: string(),
});
At runtime, we get something like this:
{
"type": "object",
"schema": {
"optionalName": {
"type": "string",
"schema": null,
},
"name": {
"type": "string",
"schema": null,
},
},
}
I don't see anything related to the optionalName
field being optional! While, we have put it in the optional(string())
function.
How should I know at runtime if a field is optional?
Finally, the type I create for the above struct is as follows:
{
optionalName: string;
name: string;
}
which I want to create it like this:
{
optionalName?: string;
name: string;
}
from superstruct.
Completed, but how?
Please give a brief explanation.
thanks a lot
from superstruct.
Related Issues (20)
- `instance` causes a TypeScript error with abstract classes HOT 1
- Unions of instances produce unhelpful error message
- Fix compatibility with Node16/NodeNext moduleResolution HOT 6
- Mask fails on `interface aaa { foo: string; [key: string]: any }` interface with any key.
- Move tests to Vitest
- Describe does not work well with union types HOT 2
- Does "create" supports "passthrough" HOT 2
- Intersection of type schema and record with enums as keys HOT 1
- [Docs] Document pass-by-reference pitfall for `defaulted` values HOT 2
- Defaulted values are not correctly copied for `record` type data. HOT 6
- Create predefined JS object from Schema HOT 5
- `Describe` incorrectly handles single literal types. HOT 1
- Simplify testing suite with Vitest
- Add tests that check that all structs that work with non-primitive values copy their data during coercion
- What does a required value mean? HOT 2
- mask does not work anymore. HOT 2
- Feature Request: Add depth Option to partial for Nested Optional Attributes
- Unexpected interactions between refined structs and coerced structs
- Coercing a refined struct doesn't trigger refinement
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 superstruct.