I'm loading a third-party library called sorted-array
and using it like this:
import SortedArray from 'sorted-array';
export class Selector {
private mySortedArray!: SortedArray;
constructor() {
this.mySortedArray = new SortedArray();
}
}
However, I get this error: Cannot use namespace 'SortedArray' as a type.ts(2709)
So, I created this file:
// src/typings/sorted-array/index.d.ts
declare module 'sorted-array' {
class SortedArray {
constructor(arr: number[]);
search(element: any): number;
}
}
However, the error remains. What am I doing wrong?
You need to export it inside module declaration:
declare module 'sorted-array' {
class SortedArray {
constructor(arr: number[]);
search(element: any): number;
}
export = SortedArray;
}
I was struggling to figure out how I could write a type definition for passing an external/3rd party module around. I am very much not wise nor sharp with TypeScript, but TypeScript 2.9's import()
syntax seems to be the answer I was looking for (after a long long long long amount of bumbling around, being misdirected):
declare type NewRelicModule = typeof import("newrelic");
Now I can write my:
interface Config {
newrelic?: NewRelicModule;
}
It looks like you are expecting to use the default export. Perhaps for you this might work?
declare type SortedArray = typeof import("sorted-array").default;
Namespaces can be converted to types using the typeof
keyword.
import * as Vector3 from './Vector3'
type Vector3 = typeof Vector3
let a:Vector3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With