Gathering detailed insights and metrics for ts-interface-checker
Gathering detailed insights and metrics for ts-interface-checker
Gathering detailed insights and metrics for ts-interface-checker
Gathering detailed insights and metrics for ts-interface-checker
dk-checker-remove-extraneous
Utility for removing extraneous params via ts-interface-checker
dk-request
Request utility with validations based on Axios & ts-interface-checker
fork-ts-checker-webpack-plugin
Runs typescript type checker and linter on separate process.
ts-interface-builder
Compile TypeScript interfaces into a description that allows runtime validation
Runtime library to validate data against TypeScript interfaces.
npm install ts-interface-checker
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
323 Stars
77 Commits
18 Forks
8 Watching
11 Branches
9 Contributors
Updated on 02 Aug 2024
TypeScript (62.51%)
JavaScript (37.49%)
Cumulative downloads
Total Downloads
Last day
-4.7%
1,848,619
Compared to previous day
Last week
2.6%
10,630,579
Compared to previous week
Last month
8.2%
44,099,093
Compared to previous month
Last year
152.8%
403,734,571
Compared to previous year
Runtime library to validate data against TypeScript interfaces.
This package is the runtime support for validators created by ts-interface-builder. It allows validating data, such as parsed JSON objects received over the network, or parsed JSON or YAML files, to check if they satisfy a TypeScript interface, and to produce informative error messages if they do not.
1npm install --save-dev ts-interface-builder 2npm install --save ts-interface-checker
Suppose you have a TypeScript file defining an interface:
1// foo.ts 2interface Square { 3 size: number; 4 color?: string; 5}
The first step is to generate some code for runtime checks:
1`npm bin`/ts-interface-builder foo.ts
It produces a file like this:
1// foo-ti.js 2import * as t from "ts-interface-checker"; 3 4export const Square = t.iface([], { 5 "size": "number", 6 "color": t.opt("string"), 7}); 8...
Now at runtime, to check if a value satisfies the Square interface:
1import fooTI from "./foo-ti"; 2import {createCheckers} from "ts-interface-checker"; 3 4const {Square} = createCheckers(fooTI); 5 6Square.check({size: 1}); // OK 7Square.check({size: 1, color: "green"}); // OK 8Square.check({color: "green"}); // Fails with "value.size is missing" 9Square.check({size: 4, color: 5}); // Fails with "value.color is not a string"
Note that ts-interface-builder
is only needed for the build-time step, and
ts-interface-checker
is needed at runtime. That's why the recommendation is to npm-install the
former using --save-dev
flag and the latter using --save
.
If you have an interface with methods, you can validate method call arguments and return values:
1// greet.ts 2interface Greeter { 3 greet(name: string): string; 4}
After generating the runtime code, you can now check calls like:
1import greetTI from "./greet-ti"; 2import {createCheckers} from "ts-interface-checker"; 3 4const {Greeter} = createCheckers(greetTI); 5 6Greeter.methodArgs("greet").check(["Bob"]); // OK 7Greeter.methodArgs("greet").check([17]); // Fails with "value.name is not a string" 8Greeter.methodArgs("greet").check([]); // Fails with "value.name is missing" 9 10Greeter.methodResult("greet").check("hello"); // OK 11Greeter.methodResult("greet").check(null); // Fails with "value is not a string"
If one type refers to a type defined in another file, you need to tell the interface checker about
all type names when you call createCheckers()
. E.g. given
1// color.ts 2export type Color = RGB | string; 3export type RGB = [number, number, number];
1// shape.ts 2import {Color} from "./color"; 3export interface Square { 4 size: number; 5 color?: Color; 6}
the produced files color-ti.ts
and shape-ti.ts
do not automatically refer to each other, but
expect you to relate them in createCheckers()
call:
1import color from "./color-ti"; 2import shape from "./shape-ti"; 3import {createCheckers} from "ts-interface-checker"; 4 5const {Square} = createCheckers(shape, color); // Pass in all required type suites. 6 7Square.check({size: 1, color: [255,255,255]});
You may check that data contains no extra properties. Note that it is not generally recommended as it this prevents backward compatibility: if you add new properties to an interface, then older code with strict checks will not accept them.
Following on the example above:
1Square.strictCheck({size: 1, color: [255,255,255], bg: "blue"}); // Fails with value.bg is extraneous 2Square.strictCheck({size: 1, color: [255,255,255,0.5]}); // Fails with ...value.color[3] is extraneous
Standard Checker
objects do the type checking logic, but are unable to make the TypeScript
compiler aware that an object of unknown
type implements a certain interface.
Basic code:
1const unk: unknown = {size: 1, color: "green"}; 2// Type is unknown, so TypeScript will not let you access the members. 3console.log(unk.size); // Error: "Object is of type 'unknown'"
With a Checker
available:
1import fooTI from "./foo-ti"; 2import {createCheckers} from "ts-interface-checker"; 3 4const {Square} = createCheckers(fooTI); 5 6const unk: unknown = {size: 1, color: "green"}; 7 8if (Square.test(unk)) { 9 // unk does implement Square, but TypeScript is not aware of it. 10 console.log(unk.size); // Error: "Object is of type 'unknown'" 11}
To enable type guard functionality on the existing test
, and strictTest
functions, Checker
objects should be cast to CheckerT<>
using the appropriate type.
Using CheckerT<>
:
1import {Square} from "./foo"; 2import fooTI from "./foo-ti"; 3import {createCheckers, CheckerT} from "ts-interface-checker"; 4 5const checkers = createCheckers(fooTI) as {Square: CheckerT<Square>}; 6 7const unk: unknown = {size: 1, color: "green"}; 8 9if (checkers.Square.test(unk)) { 10 // TypeScript is now aware that unk implements Square, and allows member access. 11 console.log(unk.size); 12}
CheckerT<>
will eventually support type assertions using the check
and strictCheck
functions,
however, this feature is not yet fully working in TypeScript.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 7/24 approved changesets -- score normalized to 2
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
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
22 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
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 More