Adding support for viz.js (#41105)

* Adding support for viz.js

* fixing type capitalization
This commit is contained in:
mckaysalisbury 2020-05-04 17:42:11 -07:00 committed by GitHub
parent 7570086826
commit b6598369a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 85 additions and 0 deletions

23
types/viz.js/common.d.ts vendored Normal file
View File

@ -0,0 +1,23 @@
interface Options {
format?: string;
engine?: string;
files?: string[];
images?: string[];
yInvert?: boolean;
}
/**
* This interface defines the shape of an object that is held by the caller.
* This `Module` was created by emscripten, and is therefore largely arcane.
* This currently just lists a subset of what is defined in `Module`.
*/
interface Module {
run(): void;
}
type RenderFunction = (instance: Module, src: string, options: Options) => string;
declare class Viz {
constructor(arg: { Module: Module; render: RenderFunction });
renderString(src: string, options?: Options): Promise<string>;
}

3
types/viz.js/full.render.js.d.ts vendored Normal file
View File

@ -0,0 +1,3 @@
/// <reference path="common.d.ts" />
export const Module: Module;
export function render(instance: Module, src: string, options: Options): string;

7
types/viz.js/index.d.ts vendored Normal file
View File

@ -0,0 +1,7 @@
// Type definitions for viz.js 2.1
// Project: https://github.com/mdaines/viz.js
// Definitions by: McKay Salisbury <https://github.com/mckaysalisbury>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="common.d.ts" />
export = Viz;

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"viz.js-tests.ts"
]
}

1
types/viz.js/tslint.json Normal file
View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@ -0,0 +1,28 @@
import Viz = require('viz.js');
import { Module, render } from 'viz.js/full.render.js';
// Correct usage
const viz = new Viz({ Module, render });
let promise: Promise<string>;
promise = viz.renderString("graph a { b }", { format: "svg" });
promise = viz.renderString("digraph a { b }");
// This won't necessarily work, but shouldn't violate typing rules
new Viz({Module, render: (instance: Module, src: string, options: Options) => "string"});
new Viz({Module, render: (instance: Module, src: string, options: {format?: string}) => "string"});
new Viz({Module, render: (instance: Module, src: string, options: {format?: string, garbage?: number}) => "string"});
viz.renderString("string", {files: []});
viz.renderString("string", {images: ["totally a file"]});
viz.renderString("string", {format: "svg", engine: "fdp", files: ["test"], images: ["totally an image"], yInvert: false});
// Incorrect
new Viz({Module: 1, render}); // $ExpectError
new Viz({Module: {}, render}); // $ExpectError
new Viz({Module, render: (instance: Module, src: string, options: Options) => 1}); // $ExpectError
new Viz({Module, render: (instance: string, src: string, options: Options) => "hello"}); // $ExpectError
new Viz({Module, render: (instance: Module, src: number, options: Options) => "string"}); // $ExpectError
new Viz({Module, render: (instance: Module, src: string, options: {format: string}) => "string"}); // $ExpectError
viz.renderString(1, {}); // $ExpectError
viz.renderString("string", { a: 1 }); // $ExpectError
viz.renderString("string", { yInvert: "true" }); // $ExpectError
viz.renderString("string", { engine: 1}); // $ExpectError