#Can I import .ts files in Astro components?

3 messages · Page 1 of 1 (latest)

graceful herald
#

Hey everyone, I was under the impression that it should be possible to import TypeScript files into Astro components, right? I can import TypeScript/JSX/React-components, but somehow a pure Typescript file does not work for me.

This is the file I'm trying to import:

interface ILanguage {
  locale: string;
  name: string;
}
export const LANGUAGES: Array<ILanguage> = [
  {
    locale: "de",
    name: "Deutsch",
  },
  {
    locale: "en",
    name: "English",
  }
];

And this is my import:

---
import { LANGUAGES } from "@config";

const allLocales = LANGUAGES.map(language => language.locale);
---

Yet, I am only getting this:

 error   allLocales is not defined
  Hint:
    export statements in `.astro` files do not have access to local variable declarations, only imported values.
  Code:
    > 1 | ---
        |   ^
      2 | import { LANGUAGES } from "@config";
      4 | const allLocales = LANGUAGES.map(language => language.locale);
#

Nevermind, this seems to be a problem inside of a dynamic route file. I can access the imported variable inside the getStaticPaths function, but not outside of it.

stark tangle
#

This is true for getStaticPaths since it executes within its own scope before the page is executed it doesnt have knowledge of what is in the scope of the page, however it can pass props out of the function to the page,