-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type error when generate declartion file for a exported schema #274
Comments
@fengkx Hi. You may need to export everything from TypeBox as many of the types (and symbols) are used for composition and inference (and TypeScript will need to know about all types in order to infer correctly) export * from '@sinclair/typebox' Does this help? |
No, It still produce the same TS4023 error.
I found this microsoft/TypeScript#37888 issue. It seems to be the same case, |
@fengkx I can't seem to reproduce the issue. modifier.tsimport { Modifier } from '@sinclair/typebox'
export { Modifier } index.tsimport { Modifier } from './modifier'
console.log(Modifier) compiled withnpx tsc index.ts --declaration # compiles without error As TypeBox already exports everything (including symbols, types, interfaces and the TypeBuilder), I don't believe there is much else the library can do to make things more public (also, re-exporting types and symbols is supported by the library (and used quite a bit in mono repository setups) so not sure what is causing the problem). I think I would need to see a full reproduction of the TS4023 error to provide more assistance. Are you able to provide a simplified reproduction of the issue? |
I make a reproduction https://github.com/fengkx/typebox-ts4023-reproduction When I make this reproduction, I found it related to
When I use this method, I got the TS4023 error
clone the repo, |
You could express class TypeBuilder extends TypeboxTypeBuilder {
public Slot(options: SchemaOptions = {}) {
return Type.Unsafe<React.ReactNode & { type: 'slot'}>({
type: 'slot', // <- this needs to be validate-able
...options,
})
}
}
export const Type = new TypeBuilder(); Just a side note, this may not be the most ideal way to use TypeBox as The following would be the intended use of // creates a nullable type understood by OpenAPI 3.1 and below.
function Nullable<T extends TSchema>(schema: T) {
return Type.Unsafe<Static<T> | null>({ // <- this is the static type to infer
...schema, nullable: true // <- this is the schema understood by Ajv
})
} The following would be a more typical usage of TypeBox when working with React. Note that there is no requirement to infer React library types. import { Type, Static } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'
export type HeaderProperties = Static<typeof HeaderProperties>
export const HeaderProperties = Type.Object({
header: Type.String({ default: 'header text' }),
subheader: Type.String({ default: 'subheader text' })
})
export function HeaderComponent(props: HeaderProperties) {
return (
<div className='header'>
<h1>{props.header}</h1>
<h2>{props.subheader}</h2>
</div>
)
}
export function App() {
return <div className='app'>
<HeaderComponent {...Value.Create(HeaderProperties)} />
</div>
} Hope this helps! |
Thanks you for detail explaination. This does solve the problem. |
I have a shared library which contains all of my schemas and shared types that I use in frontend and backend packages and I decided to list typebox as a peer dependency in that library. So far everything is working for me. (I found this issue because I thought I was having a problem with typebox schema types not resolving when exported from a shared library. The actual problem turned out to be incorrect (aliased) paths in my generated declaration files, now fixed with |
When I export the generted schema object with
Type.Optional
and havedeclaration: true
in tsconfig. I got this error.I try to add
Memtioned in this issue, but error is still there.
Because I want to export the
Modifier
, I try to add this declartion.Then I got another error
What could I do to get rid of this?
The text was updated successfully, but these errors were encountered: