https://github.com/cpnota/check-interface
Checks that an object implements all methods on a given interface in JavaScript.
https://github.com/cpnota/check-interface
Last synced: 10 months ago
JSON representation
Checks that an object implements all methods on a given interface in JavaScript.
- Host: GitHub
- URL: https://github.com/cpnota/check-interface
- Owner: cpnota
- Created: 2018-07-07T19:09:11.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-13T18:27:08.000Z (about 8 years ago)
- Last Synced: 2025-09-26T08:57:27.174Z (10 months ago)
- Language: JavaScript
- Size: 131 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
This is an npm module for checking that an object implements an interface defined by an es6 class.
This is useful for documenting interfaces separately from their implementations, for example with jsdoc.
# Installation
`npm install --save check-interface`
# Usage
```javascript
const checkInterface = require('check-interface')
class MyInterface {
constructor() {
checkInterface(this, MyInterface)
}
/**
* Documentation for MethodA
*/
MethodA() {}
/**
* Documentation for MethodB
*/
MethodB() {}
}
class MyImpl extends MyInterface {
constructor() {
super()
this.member = 'stuff'
}
MethodA() {
/* implementation of MethodA */
}
MethodB() {
/* implementation of MethodB */
}
}
class MyBadImpl extends MyInterface {
constructor() {
super()
}
MethodA() {
/* implementation of MethodA */
}
// MethodB not implemented!
}
const myImpl = new MyImpl()
console.log('worked!')
/* throws an error if the interface is not implemented */
try {
const myBadImpl = new MyBadImpl()
} catch (error) {
console.error('threw an error!')
}
/* can also use for type-checks externally */
const myImpl2 = checkInterface(myImpl, MyInterface)
/* checkInterface returns the object passed in */
assert(myImpl2 === myImpl)
console.log('worked!')
/* this is useful for initializing variables in constructors */
class OtherClass {
constructor(object) {
this.object = checkInterface(object, MyInterface)
}
}
```