Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to import an exported constant from typescript file to javascript file

I’m trying to import a constant from typescript file in javascript file but I’m always ending up with Unexpected token, expected , error.

Here is the constant in ts file:

export const articleQuery = (slug: string, categorySlug?: string) => 'context'

And I want to be able to use this function in a javascript file like this and pass the params.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import {articleQuery} from "./path/to/file.ts"

const query = articleQuery(slug, categorySlug)

I’m not sure if this is doable or not, but any recommendations to achieve this?

>Solution :

a) compile ts file with tsc path/to/file.ts into path/to/file.js and use that (general usage)

b) if you are running with tsx, ts-node or typescript --loader, just import ./path/to/file or ./path/to/file.js, that should work

c) why do you ever need to import ts from js? write a comment if you need further explanation

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading