Reputation: 440
I created an NPM package with a type defined (incl use):
(SolarEdge.js)
export type Period = 'DAY' | 'QUARTER_OF_AN_HOUR' | 'MONTH' | 'HOUR' | 'WEEK' | 'MONTH' | 'YEAR';
public getData = async (site: number, start: string, end: string, period: Period) => {}
(index.js)
export { default } from './SolarEdge';
After compiling from ts, I get 4 files (index.js, index.d.ts, SolarEdge.js, SolarEdge.d.ts)
When using this from my express app, it is complaining that I cant pass a string to this function, because it wants a Period.
Error: Argument of type 'string' is not assignable to parameter of type 'Period'.ts(2345)
Code:
const data = await solaredge.getData(
parseInt(req.params.site),
req.params.start,
req.params.end,
//'MONTH',
req.params.period,
);
req.params.period
contains a string with 'MONTH' or something else. WHen using the hardcoded string MONTH it works, but using the req.params.period variable, it doesnt.
I tried doing: req.params.period as Period
but the problem is: it cant find Period.
How does this work?
Upvotes: 0
Views: 329
Reputation: 466
TypeScript libraries that want to support TypeScript consumers must explicitly export their types as well as emit declaration files during compilation.
It looks like you're already doing this, so you should be able to explicitly import the Period
type for use in your application code:
import { getData, Period } from 'solaredge'
...
const data = await solaredge.getData(
parseInt(req.params.site),
req.params.start,
req.params.end,
//'MONTH',
req.params.period as Period,
);
Upvotes: 1