jrv
jrv

Reputation: 461

creating typescript .d.ts for third-party library

I am using a simple third-party library from npm, chi-squared-test (https://www.npmjs.com/package/chi-squared-test). It exports one (anonymous) function in (what I believe is) commonjs style:

module.exports = function(observations, expectations, degreesOfFreedomReduction) { ... }

The function returns an object with a couple of attributes, the most interesting of which to me is named "probability". I installed it using "npm i --save chi-squared-test." I am working in a component in angular 4. I am able to use the exported function with this import statement in my component.ts file:

import * as chiSquaredTest from 'chi-squared-test';

The function is used as shown here:

const expected: number[] = <omitted calculation>;
const actual: number[] = <omitted calculation>;
const chiSquaredResult: IChiSquaredResult = 
  chiSquaredTest(actual, expected, 1);

I have been trying to create a .d.ts file for this third-party library so I will have intellisense (in Visual Studio Code, if that matters). I have tried a number of variations but have either gotten compile errors or have gotten no intellisense and no indication that typescript is paying any attention to the file. My most recent trial has been to create a file 'index.d.ts' under an @types/chi-squared-test directory under the src directory.

import { IChiSquaredResult } from './IChiSquaredResult';
export default function chiSquaredTest(
  actual: number[],
  expected: number[],
  degreesOfFreedomAdjustment: number): IChiSquaredResult;

The file IChiSquaredResult has a definition for the return type, which is just an interface with with a single numeric property called "probability". This .d.ts file seems to be ignored when it is placed in the project directory structure.

If I move the directory & files over to the node_modules/@types directory, tsc pays attention it, but not helpfully. It reports, "error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'typeof "/home/jrv/Documents/projects/20171130 ObaCalculator/ObaCalc/node_modules/@types/chi-squar...' has no compatible call signatures." It seems as though tsc is finding the .d.ts file but is not linking my type definition to the anonymous function export returned by the library. tsc and intellisense also don't describe what type they do think "chiSquaredTest" is, only that my use doesn't match the signature. When I right-click, "go to type definition" it goes to my export in my .d.ts file.

I have also tried importing as

import chiSquaredTest = require( 'chi-squared-test' );

with similar results. How can a type definition be bound to an anonymous function export as with the library I am using? The code itself works fine, but I am unable to bind the @types to the imported function.

Upvotes: 4

Views: 2535

Answers (1)

jrv
jrv

Reputation: 461

I was 97.44% there. In my .d.ts file I should have used:

import { IChiSquaredResult } from './IChiSquaredResult';
declare function chiSquaredTest(actual: number[], expected: number[], degreesOfFreedomAdjustment: number): IChiSquaredResult;
export = chiSquaredTest;

I use this in conjunction with the second import style. I suspect that I can move the IChiSquaredResult internal to the .d.ts file as here: Create d.ts file for existing library exporting function, and my inability to save the .d.ts file locally is a minor issue.

Upvotes: 2

Related Questions