0

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?

3
  • 1
    Did you import that name? Commented Dec 28, 2019 at 11:10
  • 1
    Maybe the period is accessible via solaredge.Period, assuming that the type is being exported? Commented Dec 28, 2019 at 11:21
  • I created this package myself, so Im just curious how I should do that. SolarEdge.Period doesnt work. Ill add my package code in the question Commented Dec 28, 2019 at 12:26

1 Answer 1

1

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,
);
Sign up to request clarification or add additional context in comments.

1 Comment

This is the answer I had to explicitly export the type and then explicitly import in my code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.