Installations
npm install as-typed
Developer Guide
Typescript
Yes
Module System
N/A
Node Version
14.18.1
NPM Version
6.14.15
Score
91.8
Supply Chain
100
Quality
76.2
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Developer
lbguilherme
Download Statistics
Total Downloads
1,356,827
Last Day
3,069
Last Week
13,440
Last Month
64,332
Last Year
628,599
GitHub Statistics
43 Stars
75 Commits
1 Forks
3 Watching
9 Branches
1 Contributors
Package Meta Information
Latest Version
1.3.2
Package Id
as-typed@1.3.2
Unpacked Size
13.77 kB
Size
4.35 kB
File Count
5
NPM Version
6.14.15
Node Version
14.18.1
Total Downloads
Cumulative downloads
Total Downloads
1,356,827
Last day
-3%
3,069
Compared to previous day
Last week
-11.5%
13,440
Compared to previous week
Last month
2%
64,332
Compared to previous month
Last year
44.6%
628,599
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
as-typed
Type magic to convert a JSON Schema literal into the proper TypeScript type representation, all without additional build steps. This module has no runtime functionality by itself. It exposes a single AsTyped
type which takes a valid JSON Schema and outputs the equivalent type for it. With this you can get type safety at runtime and validate your values at runtime writing types just once. Great for JSON integrations and data serialization.
This is forked from https://github.com/wix-incubator/as-typed fixing many bugs, modernizing and introducing support for more types thanks to newer TypeScript features.
Install
npm install --save-dev as-typed
Usage
1import { AsTyped } from "as-typed"; 2 3const schema = { 4 title: "Example Schema", 5 type: "object", 6 required: ["firstName", "age", "hairColor"], 7 properties: { 8 firstName: { 9 type: "string" 10 }, 11 lastName: { 12 type: "string" 13 }, 14 age: { 15 type: "integer", 16 minimum: 0 17 }, 18 hairColor: { 19 enum: ["black", "brown", "blue"], 20 type: "string" 21 } 22 } 23} as const; // <<< "as const" is important to preserve literal type 24 25type SchemaT = AsTyped<typeof schema>; 26/* 27 type SchemaT = { 28 firstName: string; 29 age: number; 30 hairColor: "black" | "brown" | "blue"; 31 lastName?: string; 32 }; 33*/
Primitive and literal types
1type Str = AsTyped<{ type: "string" }>; // string 2type Num = AsTyped<{ type: "number" }>; // number 3type Int = AsTyped<{ type: "integer" }>; // number 4type Bool = AsTyped<{ type: "boolean" }>; // boolean 5type Null = AsTyped<{ type: "null" }>; // null 6 7type ConstStr = AsTyped<{ type: "string"; const: "Hello" }>; // "Hello" 8type ConstNum = AsTyped<{ type: "integer"; const: 4 }>; // 4 9type Enum = AsTyped<{ 10 type: "string"; 11 enum: ["First", "Second", "Third"]; 12}>; // "First" | "Second" | "Third" 13 14type Nullable1 = AsTyped<{ type: ["string", "null"] }>; // string | null 15type Nullable2 = AsTyped<{ type: "string"; nullable: true }>; // string | null
- Patterns are not supported. There is no regex validation in typescript, see Typescript issue 6579.
- Value validation (min, max etc) is not supported. Typescript is not meant for value checking (at least currently).
Objects
1type Obj1 = AsTyped<{ 2 type: "object"; 3 properties: { 4 foo: { type: "number" }; 5 }; 6}>; // { foo?: number } 7 8type Obj2 = AsTyped<{ 9 type: "object"; 10 properties: { 11 foo: { type: "number" }; 12 bar: { type: "string" }; 13 }; 14 required: ["foo"]; 15}>; // { foo: number, bar?: string } 16 17type Obj3 = AsTyped<{ 18 type: "object"; 19 additionalProperties: { type: "integer" }; 20}>; // Record<string, number>
Arrays and Tuples
1type List1 = AsTyped<{ 2 type: "array"; 3 items: { type: "string" }; 4}>; // string[] 5 6type List2 = AsTyped<{ 7 type: "array"; 8 items: { 9 type: "array"; 10 items: { type: "string" }; 11 }; 12}>; // string[][] 13 14type Tuple1 = AsTyped<{ 15 type: "array"; 16 items: [{ type: "string" }, { type: "number" }]; 17}>; // [string, number] 18 19type Tuple2 = AsTyped<{ 20 type: "array"; 21 items: [{ type: "number" }, { type: "string" }]; 22 additionalItems: { type: "boolean" }; 23}>; // [number, string, ...boolean[]]
References by id
1type ObjFromRefs = AsTyped<{ 2 definitions: { 3 User: { 4 $id: "userschemaid"; 5 type: "object"; 6 properties: { 7 name: { type: "string" }; 8 age: { type: "integer" }; 9 }; 10 }; 11 UserList: { 12 $id: "userlist"; 13 type: "array"; 14 items: { $ref: "userschemaid" }; 15 }; 16 }; 17 type: "object"; 18 required: ["result"]; 19 properties: { result: { $ref: "userlist" } }; 20}>; // { result: { name?: string, age?: number }[] }
References by path
1type ObjFromRefs = AsTyped<{ 2 definitions: { 3 User: { 4 type: "object"; 5 properties: { 6 name: { type: "string" }; 7 age: { type: "integer" }; 8 }; 9 }; 10 UserList: { 11 type: "array"; 12 items: { $ref: "#/definitions/User" }; 13 }; 14 }; 15 type: "object"; 16 required: ["result"]; 17 properties: { result: { $ref: "#/definitions/UserList" } }; 18}>; // { result: { name?: string, age?: number }[] }
Advanced Types
1type Union1 = AsTyped<{ anyOf: [{ type: "string" }, { type: "number" }] }>; // string | number 2 3type Union2 = AsTyped<{ oneOf: [{ type: "string" }, { type: "number" }] }>; // string | number 4 5type Intersection1 = AsTyped<{ 6 allOf: [ 7 { type: "object"; properties: { a: { type: "number" } } }, 8 { type: "object"; properties: { b: { type: "string" } } } 9 ]; 10}>; // { a?: number, b?: string } 11 12type Intersection2 = AsTyped<{ 13 allOf: [ 14 { type: "object"; properties: { a: { type: "number" } } }, 15 { 16 oneOf: [ 17 { type: "object"; properties: { b: { type: "string" } } }, 18 { type: "object"; properties: { b: { type: "boolean" } } } 19 ]; 20 } 21 ]; 22}>; // { a?: number; b?: string } | { a?: number; b?: boolean } 23 24type Not = AsTyped<{ not: { type: "string" } }>; // number | object | any[] | boolean | null | undefined
oneOf
Currently doesn"t work as expected, and resolves the same as anyOf. See Typescript issue 20863.if
/then
/else
acts exactly like{oneOf: [{allOf: [If, Then]}, Else]}
. Currently doesn't work as expected, for the same reasons as oneOf. Resolves to(If & Then) | Else
, which is not an accurate translation.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/release.yml:1
- Warn: no topLevel permission defined: .github/workflows/release_dev.yml:1
- Warn: no topLevel permission defined: .github/workflows/spec.yml:1
- Info: no jobLevel write permissions found
Reason
Found 0/14 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/lbguilherme/as-typed/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/lbguilherme/as-typed/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release_dev.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/lbguilherme/as-typed/release_dev.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release_dev.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/lbguilherme/as-typed/release_dev.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/spec.yml:8: update your workflow using https://app.stepsecurity.io/secureworkflow/lbguilherme/as-typed/spec.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/spec.yml:9: update your workflow using https://app.stepsecurity.io/secureworkflow/lbguilherme/as-typed/spec.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/release.yml:16
- Warn: npmCommand not pinned by hash: .github/workflows/release.yml:17
- Warn: npmCommand not pinned by hash: .github/workflows/release_dev.yml:16
- Warn: npmCommand not pinned by hash: .github/workflows/release_dev.yml:17
- Warn: npmCommand not pinned by hash: .github/workflows/spec.yml:13
- Info: 0 out of 6 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 5 npmCommand dependencies pinned
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 16 are checked with a SAST tool
Score
3.4
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to as-typed
safefs
Stop getting EMFILE errors! Open only as many files as the operating system supports.
make-deno-edition
Automatically makes package.json projects (such as npm packages and node.js modules) compatible with Deno.
as-typed-array
Make any value an array
ambi
Ambi lets you execute any function ambidextrously; providing you the ability to execute any function (be it synchronous, asynchronous, returns, callbacks, promises) as if it returned a promise.