Home > Software design >  How to import an exported constant from typescript file to javascript file
How to import an exported constant from typescript file to javascript file

Time:11-19

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.

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?

CodePudding user response:

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

  • Related