After declaring a type on Typescript 3.8+, it is possible to export it for use in

After defining a type in Typescript 3.8+ and attempting to export it:

type Type = { Prop: string };
export { Type }

An error message appears in VS Code:

When using the --isolatedModules flag, re-exporting a type requires using export type

To address this, I modified the code as per the error message:

type Type = { Prop: string };
export type { Type }

However, this adjustment led to another error flagged by eslint:

Parsing error: Declaration or statement expected.

I have two questions regarding this scenario:

  1. Why is this considered "re-exporting"?
  2. Is this form of export supported in Typescript 3.8+ with the --isolatedModules flag enabled, as mentioned here?

Answer №1

I successfully resolved the issue I was facing:

  1. By enabling the --isolatedModuls flag, I encountered a problem where VS Code's TypeScript language service couldn't determine whether the type we were attempting to export originated in the current module or not.

    A similar dilemma is discussed in a Babel Github issuehere:

    To ascertain if something is a type, information about other modules is required.

    import { T } from "./other-module";
    export { T }; // Is this a type export?
    

    Furthermore, a comprehensive explanation can be found on this Reddit thread:

    Type space refers to TypeScript's domain containing all types relevant to your application. These types only exist during compile time and disappear once the compilation is complete, leaving no types in the resulting JavaScript. On the other hand, value space encompasses elements that persist and survive into the JS.

    type Foo = { bar: string };
    const a: Foo = { bar: 'hello' };
    

    In this context, Foo resides in the type space while everything else falls under the value space.

    The issue with isolatedModules arises from the inability to differentiate between type and value spaces since each file is compiled independently. When you execute:

    export { Foo } from './bar';
    

    in a file, TypeScript struggles to identify whether Foo belongs to the type space (resulting in it being discarded as it won't appear in the generated JavaScript) or the value space (thus requiring inclusion in the output).

  2. The solution, as highlighted by the error message, involves the following process:

    type Type = { Prop: string };
    export type { Type }
    

    Although this approach initially triggered an eslint error per my inquiry, the error itself turned out to be inaccurate. Despite numerous attempts, I couldn't resolve the error and ended up recreating my Typescript app, which ultimately succeeded.
    For readers encountering a similar issue, ensure you adhere to the correct setup instructions:

    // initializing a typescript app and installing necessary type declarations: @types/node @types/react @types/react-dom @types/jest
    npx create-react-app hello-world --template typescript
    
    // if working with react-redux, the installation steps are as follows:
    cd hello-world
    npm install --save redux @types/redux
    npm install --save react-redux @types/react-redux 
    

    Regarding eslint, the TypeScript-specific eslint module named @typescript-eslint should be present in the node_modules directory post executing the aforementioned steps. You can verify this by declaring an unused variable:

    const a = 1;
    

    This declaration will prompt @typescript-eslint to issue a warning:

    'a' is assigned a value but is never used

    P.S.

    It's important to note that certain errors are still identified by regular eslint rather than @typescript-eslint. For instance, attempting to improperly export the variable a as shown below will trigger an error:

    const a = 1;
    export a; // results in an error
    

    ... leading to the Parsing Error originally detected by eslint.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error message: "IAngularStatic type does not have property IScope" caused by Typescript Directives

I'm working on creating an Angular Directive using TypeScript to share a scope item. I created an interface that inherits from ng.IScope, but Visual Studio Code is showing me a warning: "Property IScope does not exist on type IAngularStatic". I am usi ...

Issue with retrieving properties in Angular template due to undefined values in HTML

As a newbie to Angular, I am dedicated to improving my skills in the framework. In my project, I am utilizing three essential files: a service (Studentservice.ts) that emits an observable through the ShowBeerDetails method, and then I subscribe to this ob ...

Data that changes dynamically on a chart

When making a rest call to fetch data, I aim to populate the pieChartData with the obtained information. However, I am facing difficulties in achieving this task. Can someone guide me on how to accomplish this? import { Component, OnInit} from '@angu ...

Remove any applied sorting from the table within the code

I've been attempting to remove the applied sorting on a table programmatically using the active and direction fields, but so far, I haven't been successful: @ViewChild(MatSort) sort: MatSort; private clearSort() { // Clear the active sort c ...

What is the best way to set up Storybook with Vue Cli 3?

I'm facing difficulties installing Storybook in a Vue Cli 3 project. Every time I try to npm run storybook, I encounter this error: Cannot find module '@storybook/vue/dist/server/config/defaults/webpack.config.js' I suspect that this i ...

Exploring Next.js 13: Enhancing Security with HTTP Cookie Authentication

I'm currently working on a web app using Next.js version 13.4.7. I am setting up authentication with JWT tokens from the backend (Laravel) and attempting to store them in http-only cookies. Within a file named cookie.ts, which contains helper functio ...

An error in Typescript is indicating that a semicolon is expected. The identifier 'EventNameString' is currently being used as a value, even though it only refers to a type

I've been working on integrating Firebase phone authentication into an older Ionic project and have followed several tutorials. I was able to successfully implement it, but whenever I run ionic serve -l, I encounter the following error: Interestingly ...

Guide for Showing Data from Json Mapper in Angular 5

As a newcomer to Angular5 with TypeScript, I am trying to figure out how to display data from my JSON. I have an API that was created using Java. I have created a JSON Mapper in my Angular code like this : The JSON generated from my Java application looks ...

The value of a checkbox in Ionic 2

I have implemented the code for reading checkbox values in Ionic 2 following the answer provided. However, I encountered an error message in the console: Cannot read property 'indexOf' of undefined Here is my home.html code: <form #leadsF ...

Using Inheritance to Create Custom Event/Callback Handlers in TypeScript

Currently facing an issue with Typescript that I'm stuck on. I have created a named callback Handler class that receives the allowed list of "events"/"handlernames" as a generic: class NamedHandler<H extends { [key: string]: HandlerFunction }> ...

Passing properties to a component from Material UI Tab

I have been attempting to combine react-router with Material-UI V1 Tabs, following guidance from this GitHub issue and this Stack Overflow post, but the solution provided is leading to errors for me. As far as I understand, this is how it should be implem ...

What is the best way to retrieve TemplateRef from an Angular component?

TS Component ngOnInit() { if(somecondition) // The line of code that is causing issues this.openModal(#tempName); } HTML Component <ng-template #tempName> Content goes here! </ng-template> this.openModal(#tempNa ...

A simple way to automatically fill an input field with a mask when clicking in Angular 2

When a user clicks on this span, the following action is triggered: <span data-content="15" #Fast15 (click)="enterFastTime(Fast15)" class="quick-time">15mins</span> Users can also manually input a date in the following input field. If they ...

Here's a method to extract dates from today to the next 15 days and exclude weekends -Saturday and Sunday

Is there a way to generate an array of dates starting from today and spanning the next 15 days, excluding Saturdays and Sundays? For example, if today is 4/5/22, the desired array would look like ['4/5/22', '5/5/22', '6/5/22' ...

Can a generic type be utilized to instantiate an object?

In my code, I have a class named Entity as shown below: class Entity { constructor(readonly someValue: string) {} someFunction() {} } Now, I am trying to create a class that will handle these entities and be able to create instances of them. In or ...

What is the process of extracting information from a JSON file and how do I convert the Date object for data retrieval?

export interface post { id: number; title: string; published: boolean; flagged: string; updatedAt: Date; } ...

hiding html elements by using the display property set to none instead of physically removing

I am currently utilizing an if-else statement to display different HTML structures. As a result, the entire HTML is being rendered twice. Is there a way we can utilize 'display: none' instead? I attempted to use it in th ...

Disabling FormArray on-the-fly in Angular

I have a scenario where I need to disable multiple checkboxes in a FormArray when the page loads. Despite my attempts to implement this, it hasn't been successful so far. Can someone provide guidance on how to achieve this? .ts file public myForm: Fo ...

Implementing a Typescript hook using useContext along with an uninitialized context object

I am currently attempting to develop a custom hook called useAuth in React 17 with TypeScript. While my solution is somewhat functioning, it requires the following syntax when utilizing the hook methods: const auth = useAuth(); // Do other stuff ...

Is it advisable to pass useSelector to useState in React?

Hey everyone, I've got a question about preferences for a specific method: When working with a React functional component in TypeScript that retrieves values from Redux State using useSelector, which approach do you prefer? 1) const campaign = us ...