js-typed-object
Runtime type-checking for JavaScript objects without TypeScript or other dependencies, but with TypeScript-like syntax.
Example code:
// or, in Nodeconst createInterface createWithInterface = // Step 1. Creating an interface const personInterfaceDescriptor = name: 'string' age: 'number' married: '?boolean' numbers: '?number[]'const personInterface = // Step 2. Creating a typed object via interface aboveconst person = // Step 3. Try to use different typespersonage = '23'// TypeError: Type "string" is not assignable to type "number"
What is interface?
In this package, of course, interface
doesn't mean real
interface from OOP. You can't implement them in classes, you can't
write interface extending class. For now it is just a type
definition for object fields.
Supported types
number
(and number array vianumber[]
);string
(and string array viastring[]
);boolean
(and boolean array viaboolean[]
).
More types are coming soon! Complex types are on the way too.
Optional properties
All the types can be written like '?type'
. It means that this
property is optional, and you can set it as undefined
.
TODO
- Complex types (
string|number
); - adding more supported types (functions, objects, etc.);
- ability to use other interfaces as types;
- babel plugin.
Browser support
Most of the code in the library is well supported by browsers, however, Proxy is used to work with arrays. You can look at the proxy support table to see which browsers will be able to work without polyfills.