Troubles encountered when utilizing exported interface in Nuxt 3 and Typescript within app.vue

I am a newcomer to Nuxt 3 and facing challenges with importing an exported interface in app.vue.

The DTO I have defined is as follows:

// in ~/types/dtos
export interface UserDto {
    id: string; 
    email: string;
}

There is an index.ts file in ~/types/dtos:

// ~/types/dtos/index.ts
export * from "./UserDto";

In my app.vue, when I import it and use it like this:

<script setup type="ts">
import {UserDto} from "~/types/dtos";

const user = ref<UserDto>({
  id: "abc123",
  email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a2a1a0f2f1f083a6bba2aeb3afa6eda0acae">[email protected]</a>"
});
console.log(user);
</script>

An error appears in the browser:

Uncaught SyntaxError: The requested module 'http://localhost:3000/_nuxt/types/dtos/index.ts' doesn't provide an export named: 'UserDto'

Additionally, I receive a lint error stating that it is only being used as a type, so it should be imported as a type. When I attempt to do so, I encounter another error (specifically within app.vue):

Vue: import type declarations can only be used in TypeScript files.

Lastly, I have configured an alias for the dtos which still does not work in app.vue:

  alias: {
    "@dtos": "~/types/dtos",
  },

If I perform the same actions in any Page within ~/pages, everything works smoothly.

Question 1: Is there a special step I need to take to make it function properly in the app.vue?

Question 2: Why am I encountering that error when the setup script is typed as Typescript?

Thank you in advance for any assistance or guidance provided.

Answer №1

when utilizing UserDto as a TypeScript interface, make sure to import it as a type:

import type { UserDto } from "~/types/dtos";

alternatively,

const user = ref({} as UserDto);

then incorporate it into your code:

const user = ref<UserDto>({
id:'abc123,
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb8a8988dad9d8ab8e938a869b878c888685">[email protected]</a>'
})

by using import type, the compiler will import it for type validation and exclude it from the final JavaScript bundle

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

Choosing an element that does not have a particular string in its colon attribute name

In my code, I have multiple elements structured like this: <g transform="matrix"> <image xlink:href="data:image/png" width="48"> </g> <g transform="matrix"> <image xlink:href="specific" width="48"> </g> <g tran ...

An error has occurred due to a syntax issue with the unexpected

(For the full ASP.Net Core + Angular Universal code, visit this link) Encountering an error when running my prerendered SPA using Angular Universal with "dotnet run" and navigating to any route. This issue does not occur when I execute it in the ClientApp ...

Error in Typescript: The property 'children' is not included in the type but is necessary in the 'CommonProps' type definition

Encountering this error for the first time, so please bear with me. While working on a project, I opened a file to make a change. However, instead of actually making any changes, I simply formatted the file using Prettier. Immediately after formatting, t ...

Incorporating TypeScript into a React-Native Expo development venture

While attempting to integrate TypeScript into a React-Native Expo project, I encountered an error when renaming a file from abc.js to abc.tsx: I have been following the instructions provided at: https://facebook.github.io/react-native/blog/2018/05/07/u ...

Error: The `ngMetadataName` property cannot be accessed because it is undefined or null in Internet Explorer version 10

Encountered an issue in IE 10 that is not present in IE 11: Error: TypeError: Unable to get property 'ngMetadataName' of undefined or null reference The property ngMetadataName can be found in the file vendor.js. This is the content of polyf ...

Avoid using the --transpileOnly option as it may cause issues, and refrain from using the --ignoreWatch option as

Having some issues running a script on my node server using ts-node-dev with the parameters --transpileOnly and --ignoreWatch. Unfortunately, I keep encountering errors: https://i.sstatic.net/8HxlK.png Here is a snippet from my package.json: https://i.s ...

Triggering TypeScript error due to missing type annotations for arrays

When I type const array = [], TypeScript automatically infers it as any[]. I have been looking for a solution to make this fail linting, but so far I have not found any rule in either ESLint or TypeScript that can help with this. const array = []; //arra ...

Angular mat-select is having difficulty displaying options correctly on mobile devices or devices with narrow widths

In my Angular project, I've encountered an issue with mat-select when viewing options on mobile or low-resolution screens. While the options are still displayed, the text is mysteriously missing. I attempted to set the max width of the mat-option, but ...

Using Typescript alongside Angular 1.6 and browserify for your development needs

Currently navigating the world of working with Angular types in TypeScript and bundling code using Browserify. After following a TypeScript guide related to Gulp, I utilized npm to install the Angular types and put together this simple ts file. import * a ...

Using static JSON files in create-react-app with TypeScript

I'm currently diving into the world of TypeScript and attempting to import a basic JSON file that I have stored locally in a project created with create-react-app. The contents of data.json are as follows: { "test": "123", } Within my App.tsx fil ...

Troubleshooting: Service in Angular fails to communicate data

I am facing a challenge in using a service to enable communication between two components that are not directly related. I am unsure about how to utilize @Injectable and providedIn, which makes me think that the issue may lie there. However, everything I f ...

Creating an overloaded callable interface using TypeScript

The thread on implementing a callable interface provides some helpful information, but it doesn't fully address my specific query. interface lol { (a: number): (b: number) => string // (a: string): (b: string) => string // overloaded wi ...

Load a React component in the same spot when clicked

Is there a way to implement a functionality where clicking on different buttons in a panel will display different components in the same location? <AddNotification /> <EditNotification /> <DeleteNotification /> const AdminPanel = () =& ...

The 'SVGResize' or 'onresize' property is not available on the 'SVGProps<SVGSVGElement>' type

Using React with SVG I'm facing an issue with handling the resizing event of an svg element. I have looked into using the SVGResize and onresize events, but encountered compilation errors when trying to implement them: const msg1 = (e: any) => co ...

Refreshing Vue components based on changed data using asynchronous methods

In my application, there is an icon that represents a favorite or not favorite status with either a red or white heart. The code snippet below shows how the image source changes based on the favorite status: <img :src="favoriteStatus ? '/icon ...

The table component in Primeng is encountering issues when being used with ngFor

I'm attempting to iterate through an array where each object represents a table in HTML, and it should be displayed like this: <p-table [value]="section" *ngFor="let section of sections"> <ng-template pTemplate="header"> <t ...

Making changes to a property within a class does not automatically reflect those changes in the corresponding view

In my Typescript (.ts) file, this is the code I have: private today: Date = new Date(); And this is the corresponding HTML: <span [innerText]="today | date:dateFormat"></span> Everything displays perfectly, showing 22nd May. Now, I&apos ...

waiting for the import statement in a React/NextJS/Typescript project to resolve

While working on my node.js development server, I encountered a problem with the following code: import { useRouter } from 'next/router' import nextBase64 from 'next-base64'; const Load = () => { const router = useRouter() co ...

I am encountering an issue where the `this.http.post` method in Angular 4 is not sending parameters

I have been working on setting up a login system and am trying to send the parameters to the PHP server using my Angular app. Here is a snippet of my code: return this.http.post<any>('/s/login/index.php', { username: userna ...

Does the value of an Angular reactive form control only reflect what the user inputs?

I am working with a reactive form where I need to extract values and assign them to a variable defined in an interface. The form is populated using the Google Places API, which disables user interaction with all controls except for the initial address inpu ...