ecyrbe / zodios

typescript http client and server with zod validation
https://www.zodios.org/
MIT License
1.59k stars 44 forks source link

How to split into different files and keep type safety? #584

Closed Farnsi closed 2 months ago

Farnsi commented 5 months ago

Error: Property 'getFiles' does not exist on type 'ZodiosInstance<any[]>'.ts(2339)

Example

api.ts

import {  makeApi } from '@zodios/core'
import files from './files'

export const api = mergeApis({
  '/files': files
})

files.ts

// ...
export default apiBuilder({
  method: 'get',
  path: '/',
  alias: 'getFiles',
  description: 'Get all files',
  response: FilesSchema,
  errors: FilesErrors,
}).build()

test.ts

import { api as apiDefinition } from './api'
// ...
const api = new Zodios(url, apiDefinition)
const response = await api.getFiles()
//                         ^^^^^^^^ Property 'getFiles' does not exist on type 'ZodiosInstance<any[]>'.ts(2339)

The request works, only the type error occurs.

When I put files.ts content into api.ts everything is ok.

@zodios/core: 10.9.6 zod: 3.22.4 typescript: 5.3.3

tsconfig.json

{
  "compilerOptions": {
    "composite": true,                                  
    "target": "ESNext",                                 
    "experimentalDecorators": true,                     
    "module": "ESNext",                                 
    "moduleResolution": "node",                         
    "noEmit": true,                                     
    "isolatedModules": true,                            
    "allowSyntheticDefaultImports": true,               
    "esModuleInterop": true,                            
    "verbatimModuleSyntax": true,                       
    "strict": true,                                     
    "skipLibCheck": true                               ,
  }
}
stale[bot] commented 4 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

atrick-speedline commented 4 months ago

@Farnsi check your other typescript errors. I'm not sure if this is you're problem, but it seems like you should be importing mergeApis in api.ts.

stale[bot] commented 3 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.