When a type is exported from a Typescript module, it can be of

I am facing an issue with exporting a complex type created using the typeof operator in Typescript. Here is the scenario:

// controller/createProfile.ts

import { z } from 'zod';
import { zValidator } from '@hono/zod-validator';

const createProfileBody = z.object({
  id: z.string({
    required_error: 'Id is required.',
  }),
  username: z.string({
    required_error: 'User name is required.',
  }),
  firstname: z.string({
    required_error: 'First name is required',
  }),
  lastname: z.optional(z.string()),
  avatar: z.optional(z.string()),
});

const route = app.post('/', zValidator('json', createProfileBody), async c => {
  ...
});

export type CreateProfileType = typeof route;
// types.d.ts
export * from './controller/createProfile';

The exported type CreateProfileType looks like this:

Hono<{
    Bindings: Binding;
}, Schema<"post", "/", {
    json: {
        lastname?: string | undefined;
        avatar?: string | undefined;
        id: string;
        username: string;
        firstname: string;
    };
}, ReturnType>>

When trying to import the CreateProfileType into another module, the imported type always defaults to any. I have specified the types field in my package.json and tried different configurations in the tsconfig, but the issue persists.

Edit

After generating a declaration file using tsc, I found that the type of route is lost in the export process. Manually specifying the type of route seems to resolve the issue, but it's not an ideal solution. Is there a way to export the correct type of route for accessibility in different packages?

Answer №1

My question has finally been resolved.
The reason why the type exported was set to any was due to my configuration in the tsconfig file where I had mistakenly defined the baseURL as src. Once I switched to using absolute imports, the correct type was inferred. It seems that typescript was having trouble locating the definitions for ReturnType and Binding, which affected the type inference for the variable route.

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

Troubles with using the Map function on JSON objects in ReactJS

I am currently working with React using TypeScript. I have an external JSON file that contains data needed to generate elements on the front end. There is also a button, and I want the elements to be created only when the user enables this button. However, ...

Typescript objects may contain keys that are dependent on certain parameters

I have a challenge with constructing an object that requires querying multiple database tables, resulting in a time-consuming process. To address this issue, clients of the object need to specify which specific parts they require. For example, let's c ...

Node.js server containerized with Docker: deleted express route remains accessible

I recently developed a Twitch Chat Bot using Dockerized (docker compose), Node.js v16 with express. To create an authorize-page for users to authorize my bot on the Twitch API, I utilized the route /auth/request: this.serverUrl = serverUrl; this.port = po ...

Error message: The property this.$store is not defined in Vue.js and vuex

I have come across similar questions with comparable titles, but I find them too complex to follow. I believe that by sharing my code, it will facilitate finding a solution for me. Below is the relevant code snippet. This is my store setup: Note: I have i ...

Developing an npm library with ReactJs: A step-by-step guide

As a newcomer to React, I am eager to create my own npm library in ReactJS. Taking inspiration from my existing React project, the goal is to transform it into a package (or library) that can be easily integrated into other projects. This means allowing ...

The NgRx Store encountered an error: Unable to freeze

I am currently developing a basic login application using Angular, NgRx Store, and Firebase. I have implemented a login action that is supposed to log in to Firebase and store the credentials in the store. However, it seems like there is an issue in my imp ...

In order to effectively manage the output of these loaders, it may be necessary to incorporate an extra loader. This can be achieved by using the

I'm currently working with react typescript and trying to implement a time zone picker using a select component. I attempted to utilize the npm package react-timezone-select, but encountered some console errors: index.js:1 ./node_modules/react-timezo ...

Creating dynamic class fields when ngOnInit() is called in Angular

I am trying to dynamically create variables in a class to store values and use them in ngModel and other places. I understand that I can assign values to variables in the ngOnInit() function like this: export class Component implements OnInit{ name: st ...

What is the best way to create a dynamic icon using React and TypeScript?

Excuse me, I am new to using React and TypeScript. I'm having trouble getting the icon to change based on the status in AppointmentType. The body color is working fine, but the icons are not changing. Can someone please help me solve this issue? const ...

Unable to initialize the ng2-admin Angular2 Bootstrap template on a Go server due to a failed download attempt

Hello everyone, I am a newcomer to this forum and have limited experience with Angular2 and Golang. I am currently facing an issue as I want to experiment with a template on a Go server. To tackle this, I created a main.go file that includes the followin ...

The Vue component's TypeScript object prop type does not match the expected value

Having trouble with the type of Object properties in Vue Single File Components using TypeScript (created with Vue CLI 3)? Check out the example below to see the issue. The type of this.product is currently showing as (() => any) | ComputedOptions<a ...

How to assign a value to a component variable using props in Vue.js

I am attempting to assign a props value to my component variable. Below is the code I'm working with: export default { props: { // eslint-disable-next-line vue/require-default-prop multiPaginatedTableTitle: { type: Stri ...

Check the status when clicking on an event using Angular Material

I am working with an Angular element that involves a checkbox. <mat-checkbox class="btn-block" labelPosition="before" (change)="showOptions($event)" (click)="makeJSON($event.checked,i,j,k)"> </mat-chec ...

AngluarFire 2 authState function displaying null after refreshing the page

Currently, I am working on integrating Firebase with AngularFire 2 and facing an issue. The problem arises when I try to refresh the page, as the auth instance returns null. Below is the code snippet for my AuthService: Everything functions correctly, b ...

Calculating the total of all values in a table

For my ngFor loop, the invoice total is calculated based on price and hours, but I also want to calculate the totals of all invoices in the end. <tr *ngFor="let invoice of invoiceItem.rows"> <td>{{ invoice.rowName }}</td> <td& ...

Creating XML templates in Angular 7: A comprehensive guide

How do I pass XML values in Angular 7 when the API requires this specific format of XML code? -modifydata "<datasets><dataset select=\""always\""> <replace match=\""Letter/@FName\"" value=\""Nazeeeeeeeeeeeeer\" ...

What is the proper way to reference a computed symbol that has been inherited as a function in an extended class

As a newcomer to Typescript, my understanding of the code might be lacking. I am currently working with the Klasa framework, which is built on top of Discord bot framework using Discord.js. The framework recently introduced plugin functionality and there a ...

How can I display data both as a dropdown and an autocomplete in Angular using a textbox?

There is a textbox with autocomplete functionality. When the user clicks on the textbox, an API call is made with two parameters - Pubid and Date. The data is then displayed in a dropdown with autocomplete feature. Now I am attempting to have the data app ...

Leverage environment variables within your index.html file

Currently, I am using Angular and I am encountering an issue while attempting to utilize an environment variable in my index.html for Google Analytics. I have attempted the following approach: <script> import {environment} from "./environments/e ...

The attribute specified is not present on the element within the array

I'm attempting to create an array that includes an object with initialized properties and a number. Yet, I encounter this error message: The error states: 'Property 'foo' does not exist on type 'number | IObj'. The proper ...