https://github.com/lyohaplotinka/js-typed-object
Runtime type-checking for JavaScript objects without TypeScript or other dependencies, but with TypeScript-like syntax
https://github.com/lyohaplotinka/js-typed-object
interface js type
Last synced: 3 months ago
JSON representation
Runtime type-checking for JavaScript objects without TypeScript or other dependencies, but with TypeScript-like syntax
- Host: GitHub
- URL: https://github.com/lyohaplotinka/js-typed-object
- Owner: lyohaplotinka
- License: mit
- Created: 2020-09-27T10:24:35.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-09-28T15:00:25.000Z (over 4 years ago)
- Last Synced: 2025-03-13T02:09:28.449Z (3 months ago)
- Topics: interface, js, type
- Language: JavaScript
- Homepage:
- Size: 112 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# js-typed-object
Runtime type-checking for JavaScript objects
without TypeScript or other dependencies, but with TypeScript-like
syntax.#### Example code:
```javascript
import { createInterface, createWithInterface } from 'js-typed-object'
// or, in Node
const { createInterface, createWithInterface } = require('js-typed-object')// Step 1. Creating an interface
const personInterfaceDescriptor = {
name: 'string',
age: 'number',
married: '?boolean',
numbers: '?number[]'
}
const personInterface = createInterface(personInterfaceDescriptor, 'Person')// Step 2. Creating a typed object via interface above
const person = createWithInterface(personInterface, {
name: 'Alex',
age: 23
})// Step 3. Try to use different types
person.age = '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 via `number[]`);
* `string` (and string array via `string[]`);
* `boolean` (and boolean array via `boolean[]`).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](https://caniuse.com/proxy) to see which browsers will be able
to work without polyfills.