1.0.5 • Published 5 years ago

@codexsoft/function-implements-interface v1.0.5

Weekly downloads
6
License
ISC
Repository
github
Last release
5 years ago

function-implements-interface

npm install @codexsoft/function-implements-interface

Allows runtime-check that object implements an interface (missing functionality in TypeScript)

let actualObj = {x: 'asd', y: 42};
let expectedInterface = {x: 'string'};
implementsInterface(actualObj, expectedInterface);
// returns true

interface objects are regular javascript objects where values are expected types.

x: "function" will expect that x is function

x: "string" will expect that x is string

x: "number" will expect that x is number

x: "object" will expect that x is object

x: "array" will expect that x is array

x: ["number", "string"] will expect that x is number OR string

x: {y: "number", z: "string""} will expect that x is object and implements provided interface structure (recursive)

note, that in js every array is object, but not every object is array.

for typescript

import {implementsInterface} from "@codexsoft/function-implements-interface";
 
// regular typescript interface
interface SomeInterface {
    x: string;
}

// interface structure object
let someInterface = {
    x: 'string',
}

let obj = {x: 'hello world'};
if (implementsInterface<SomeInterface>(obj, someInterface)) {
// typescript will know that obj IS SomeInterface
    console.log(obj.x); 
}
1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago