Skip to content

Commit

Permalink
chore: update index
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 16, 2023
1 parent a69ea79 commit 907c14e
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 117 deletions.
2 changes: 1 addition & 1 deletion README.ja.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.ko.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.zh-CN.md

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions questions/26401-medium-json-schema-to-typescript/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<!--info-header-start--><h1>JSON Schema to TypeScript <img src="https://img.shields.io/badge/-medium-d9901a" alt="medium"/> <img src="https://img.shields.io/badge/-%23JSON-999" alt="#JSON"/></h1><blockquote><p>by null <a href="https://github.com/aswinsvijay" target="_blank">@aswinsvijay</a></p></blockquote><p><a href="https://tsch.js.org/26401/play" target="_blank"><img src="https://img.shields.io/badge/-Take%20the%20Challenge-3178c6?logo=typescript&logoColor=white" alt="Take the Challenge"/></a> </p><!--info-header-end-->

Implement the generic type JSONSchema2TS which will return the TypeScript type corresponding to the given JSON schema.

Additional challenges to handle:
* additionalProperties
* oneOf, anyOf, allOf
* minLength and maxLength


<!--info-footer-start--><br><a href="../../README.md" target="_blank"><img src="https://img.shields.io/badge/-Back-grey" alt="Back"/></a> <a href="https://tsch.js.org/26401/answer" target="_blank"><img src="https://img.shields.io/badge/-Share%20your%20Solutions-teal" alt="Share your Solutions"/></a> <a href="https://tsch.js.org/26401/solutions" target="_blank"><img src="https://img.shields.io/badge/-Check%20out%20Solutions-de5a77?logo=awesome-lists&logoColor=white" alt="Check out Solutions"/></a> <!--info-footer-end-->
Original file line number Diff line number Diff line change
@@ -1 +1 @@
type JSONSchema2TS<T> = any;
type JSONSchema2TS<T> = any
224 changes: 112 additions & 112 deletions questions/26401-medium-json-schema-to-typescript/test-cases.ts
Original file line number Diff line number Diff line change
@@ -1,160 +1,160 @@
import { Equal, Expect } from "@type-challenges/utils";
import type { Equal, Expect } from '@type-challenges/utils'

//+ Primitive types
// + Primitive types
type Type1 = JSONSchema2TS<{
type: "string";
}>;
type Expected1 = string;
type Result1 = Expect<Equal<Type1, Expected1>>;
type: 'string'
}>
type Expected1 = string
type Result1 = Expect<Equal<Type1, Expected1>>

type Type2 = JSONSchema2TS<{
type: "number";
}>;
type Expected2 = number;
type Result2 = Expect<Equal<Type2, Expected2>>;
type: 'number'
}>
type Expected2 = number
type Result2 = Expect<Equal<Type2, Expected2>>

type Type3 = JSONSchema2TS<{
type: "boolean";
}>;
type Expected3 = boolean;
type Result3 = Expect<Equal<Type3, Expected3>>;
//- Primitive types
type: 'boolean'
}>
type Expected3 = boolean
type Result3 = Expect<Equal<Type3, Expected3>>
// - Primitive types

//+ Enums
// + Enums
type Type4 = JSONSchema2TS<{
type: "string";
enum: ["a", "b", "c"];
}>;
type Expected4 = "a" | "b" | "c";
type Result4 = Expect<Equal<Type4, Expected4>>;
type: 'string'
enum: ['a', 'b', 'c']
}>
type Expected4 = 'a' | 'b' | 'c'
type Result4 = Expect<Equal<Type4, Expected4>>

type Type5 = JSONSchema2TS<{
type: "number";
enum: [1, 2, 3];
}>;
type Expected5 = 1 | 2 | 3;
type Result5 = Expect<Equal<Type5, Expected5>>;
//- Enums
type: 'number'
enum: [1, 2, 3]
}>
type Expected5 = 1 | 2 | 3
type Result5 = Expect<Equal<Type5, Expected5>>
// - Enums

//+ Object types
// + Object types
type Type6 = JSONSchema2TS<{
type: "object";
}>;
type Expected6 = Record<string, unknown>;
type Result6 = Expect<Equal<Type6, Expected6>>;
type: 'object'
}>
type Expected6 = Record<string, unknown>
type Result6 = Expect<Equal<Type6, Expected6>>

type Type7 = JSONSchema2TS<{
type: "object";
properties: {};
}>;
type Expected7 = {};
type Result7 = Expect<Equal<Type7, Expected7>>;
type: 'object'
properties: {}
}>
type Expected7 = {}
type Result7 = Expect<Equal<Type7, Expected7>>

type Type8 = JSONSchema2TS<{
type: "object";
type: 'object'
properties: {
a: {
type: "string";
};
};
}>;
type: 'string'
}
}
}>
type Expected8 = {
a?: string;
};
type Result8 = Expect<Equal<Type8, Expected8>>;
//- Object types
a?: string
}
type Result8 = Expect<Equal<Type8, Expected8>>
// - Object types

//+ Arrays
// + Arrays
type Type9 = JSONSchema2TS<{
type: "array";
}>;
type Expected9 = unknown[];
type Result9 = Expect<Equal<Type9, Expected9>>;
type: 'array'
}>
type Expected9 = unknown[]
type Result9 = Expect<Equal<Type9, Expected9>>

type Type10 = JSONSchema2TS<{
type: "array";
type: 'array'
items: {
type: "string";
};
}>;
type Expected10 = string[];
type Result10 = Expect<Equal<Type10, Expected10>>;
type: 'string'
}
}>
type Expected10 = string[]
type Result10 = Expect<Equal<Type10, Expected10>>

type Type11 = JSONSchema2TS<{
type: "array";
type: 'array'
items: {
type: "object";
};
}>;
type Expected11 = Record<string, unknown>[];
type Result11 = Expect<Equal<Type11, Expected11>>;
//- Arrays
type: 'object'
}
}>
type Expected11 = Record<string, unknown>[]
type Result11 = Expect<Equal<Type11, Expected11>>
// - Arrays

//+ Mixed types
// + Mixed types
type Type12 = JSONSchema2TS<{
type: "object";
type: 'object'
properties: {
a: {
type: "string";
enum: ["a", "b", "c"];
};
type: 'string'
enum: ['a', 'b', 'c']
}
b: {
type: "number";
};
};
}>;
type: 'number'
}
}
}>
type Expected12 = {
a?: "a" | "b" | "c";
b?: number;
};
type Result12 = Expect<Equal<Type12, Expected12>>;
a?: 'a' | 'b' | 'c'
b?: number
}
type Result12 = Expect<Equal<Type12, Expected12>>

type Type13 = JSONSchema2TS<{
type: "array";
type: 'array'
items: {
type: "object";
type: 'object'
properties: {
a: {
type: "string";
};
};
};
}>;
type: 'string'
}
}
}
}>
type Expected13 = {
a?: string;
}[];
type Result13 = Expect<Equal<Type13, Expected13>>;
//- Mixed types
a?: string
}[]
type Result13 = Expect<Equal<Type13, Expected13>>
// - Mixed types

//+ Required fields
// + Required fields
type Type14 = JSONSchema2TS<{
type: "object";
type: 'object'
properties: {
req1: { type: "string" };
req1: { type: 'string' }
req2: {
type: "object";
type: 'object'
properties: {
a: {
type: "number";
};
};
required: ["a"];
};
add1: { type: "string" };
type: 'number'
}
}
required: ['a']
}
add1: { type: 'string' }
add2: {
type: "array";
type: 'array'
items: {
type: "number";
};
};
};
required: ["req1", "req2"];
}>;
type: 'number'
}
}
}
required: ['req1', 'req2']
}>
type Expected14 = {
req1: string;
req2: { a: number };
add1?: string;
add2?: number[];
};
type Result14 = Expect<Equal<Type14, Expected14>>;
//- Required fields
req1: string
req2: { a: number }
add1?: string
add2?: number[]
}
type Result14 = Expect<Equal<Type14, Expected14>>
// - Required fields

0 comments on commit 907c14e

Please sign in to comment.