superstruct icon indicating copy to clipboard operation
superstruct copied to clipboard

How can I generate declaration file from superstruct

Open hemedani opened this issue 1 year ago • 2 comments

I want to create .d.ts file from superstruct for example I want to convert:

const user = object({
    id: number(),
    name: string(),
  })

to

  type user = {
    id: number;
    name: string;
  };

And write it to a file with Node.js or Deno, but Infer<typeof user> does not available in runtime.

hemedani avatar May 19 '24 08:05 hemedani

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;
}

hemedani avatar May 25 '24 06:05 hemedani

typeof user.TYPE

michalCapo avatar May 30 '24 08:05 michalCapo

Completed, but how? Please give a brief explanation. thanks a lot

hemedani avatar Jul 14 '24 08:07 hemedani

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

michalCapo avatar Jul 14 '24 18:07 michalCapo