Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-schema-to-ts

Package Overview
Dependencies
Maintainers
1
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-schema-to-ts - npm Package Compare versions

Comparing version 0.1.9 to 0.1.10

2

package.json
{
"name": "json-schema-to-ts",
"version": "0.1.9",
"version": "0.1.10",
"description": "Infer typescript types from your JSON schemas!",

@@ -5,0 +5,0 @@ "main": "src/index.ts",

@@ -89,2 +89,41 @@ # Stop typing twice 🙅‍♂️

### Const
```typescript
const fooSchema = {
const: "foo",
} as const;
type Foo = FromSchema<typeof fooSchema>;
// => "foo"
```
### Enums
```typescript
const enumSchema = {
enum: [true, 42, { foo: "bar" }],
} as const;
type Enum = FromSchema<typeof enumSchema>;
// => true | 42 | { foo: "bar"}
```
You can also go full circle with typescript `enums`.
```typescript
enum Food {
Pizza = "pizza",
Taco = "taco",
Fries = "Fries",
}
const enumSchema = {
enum: Object.values(Food),
} as const;
type Enum = FromSchema<typeof enumSchema>;
// => Food
```
### Litterals

@@ -101,2 +140,15 @@

You can also specify several types:
```typescript
const litteralsSchema = {
type: ["null", "string"],
} as const;
type Litterals = FromSchema<typeof litteralsSchema>;
// => null | string
```
For `object` and `array` types, properties like `required` or `additionalItems` will also work 🙌
### Objects

@@ -225,65 +277,2 @@

### Multiple Types
```typescript
const multipleTypesSchema = {
type: ["null", "string"],
} as const;
type Tuple = FromSchema<typeof multipleTypesSchema>;
// => null | string
```
Other properties like `required` or `additionalItems` will also work 🙌
```typescript
const multipleTypesSchema = {
type: ["array", "object"],
items: [{ type: "string" }],
additionalItems: false,
properties: {
name: { type: "string" },
age: { type: "number" },
},
required: ["name"],
} as const;
type Tuple = FromSchema<typeof multipleTypesSchema>;
// => [] | [string] | { name: string, age?: number }
```
### Const
```typescript
const fooSchema = {
const: "foo",
} as const;
type Foo = FromSchema<typeof fooSchema>;
// => "foo"
```
### Enums
```typescript
const enumSchema = {
enum: [true, 42, { foo: "bar" }],
} as const;
type Enum = FromSchema<typeof enumSchema>;
// => true | 42 | { foo: "bar"}
```
`enum` can be used concurrently with `type`.
```typescript
const enumSchema = {
type: "string",
enum: ["foo", "bar", { foo: "bar" }],
} as const;
type Enum = FromSchema<typeof enumSchema>;
// => "foo" | "bar"
```
### AnyOf

@@ -290,0 +279,0 @@

@@ -125,5 +125,5 @@ import { FromWriteableSchema } from "./index";

*/
type IsLongerThan<T extends any[], N, R = true> = {
continue: T["length"] extends N ? IsLongerThan<Tail<T>, N> : false;
stop: T["length"] extends N ? R : false;
type IsLongerThan<T extends any[], N, R = false> = {
continue: T["length"] extends N ? true : IsLongerThan<Tail<T>, N>;
stop: T["length"] extends N ? true : R;
}[T extends [any, ...any[]] ? "continue" : "stop"];

@@ -130,0 +130,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc