Comments (17)
@Gounlaf Hi, Thanks for letting me know. Just had a look and yes indeed, that is quite the extreme level of breakage. I'll do some reading up on these changes and see what can be done to resolve.
Many Thanks
from typebox.
@apyrkh Glad the updates resolved those TS 3.9.2 issues :)
As for the fast-json-stringify
, had a quick look, and it may be that fast-json-stringify
is being too strict on the type
property for enum
which I believe is optional to support varying enum value types. TypeBox currently does not emit a type
property for enum schemas as TS enums values may either be number | string
(tho it is expected its going to be one or the other).
The current definition of TEnum
is as follows.
export type TEnum<T extends string | number> = { enum: Array<T> } & UserDefinedOptions
With reference to.
https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values
In all, a change to TypeBox enums are a bit tangential to this particular issue. If you're ok with these updates to resolve this TS 3.9.2 issue, I would be pretty keen to close off this one and happy to review a possible enum
type change in a separate GH issue thread.
Cheers
S
from typebox.
Hi @sinclairzx81,
Sorry, I still not have tested your latest version.
I will soon.
from typebox.
Yes, the same for me :(
from typebox.
I was to report the same. All of the projects just stopped building. You can find some reference here microsoft/TypeScript#38460 and here microsoft/TypeScript#38571
Let me know if you need help!
from typebox.
Thanks @fox1t
Yeah, its quite the regression. Had a look early on today, but havent fully grasped the implications of the 3.9 changes. Would certainly be open to any and all assistence !!!
I dont antipicipate im going to be able to get around to resolving this within the next few days at least. I'd certainly be willing to accept any PRs in the interim however.
Apologies for the inconvenience. Will keep an eye on those issues youve linked. Thanks again.
from typebox.
It seems that is side effect of this behavior microsoft/TypeScript#37195
some more context: microsoft/TypeScript#38542
from typebox.
Just published a fix that should resolve the build errors on TS 3.9.2. Updates are published on @sinclair/[email protected]
.
The issues were around trying to flatten readonly, optional modifier objects into a presentable object for intellisense. This has been removed for the time being, the types should still work as to spec.
const T = Type.Object({
a: Type.ReadonlyOptional(Type.String()),
b: Type.Readonly(Type.String()),
c: Type.Optional(Type.String()),
d: Type.String()
})
type S = Static<typeof T>
Pre 3.9.2
type S = {
readonly a?: string
readonly b: string
c?: string
d: string
}
Post 3.9.2
type S = {
readonly a?: string;
} & {
readonly b: string;
} & {
c?: string;
} & {
d: string;
}
If you're able to test your side, that would be great.
from typebox.
@sinclairzx81
Thank you!!
The initial problem has gone but now there is one more:
import { Type } from '@sinclair/typebox';
import fastJson from 'fast-json-stringify';
enum Enum {
A = 'A',
B = 'B',
C = 'C',
}
const schema = Type.Object({
param1: Type.String(),
enum: Type.Enum(Enum, { type: 'string' }),
});
const stringify = fastJson(schema); // <=== this string doesn't pass ts type validation
stringify({});
I don't know whether the issue in fast-json-stringify
or in your package
from typebox.
I found out a workaround how to define Enum:
const schema = Type.Object({
param1: Type.String(),
enum: {
enum: Object.values(Enum),
type: 'string',
},
});
from typebox.
Error TS2315 disappear :)
Now I have issue with TS2589 (Type instantiation is excessively deep and possibly infinite). I have to figure out why =/
from typebox.
Don't know it's related to typebox or not;
const FooTypeSchema = Type.Object({
a: Type.String(),
b: Type.Boolean(),
})
export type FooType = Static<typeof FooTypeSchema>
// This one is defined manually without schema
export type BarType = {
c: string;
d: boolean;
}
export function convertFoo(foo: FooType): BarType {
return {
c: foo.a,
d: foo.b
}
}
// this one triggers TS2589
export function buggyConvertFoos(...foos: FooType[]): BarType[] {
return foos.map(foo => convertFoo(foo))
}
// this one doesn't
export function convertFoos(...foos: FooType[]): BarType[] {
const map: BarType[] = []
foos.forEach(foo => map.push(convertFoo(foo)))
return map
}
I don't know how to find if it's related to typebox or not.
Any idea?
from typebox.
@Gounlaf Thanks for the repro.
At this stage, I really have no idea if this is a TypeBox issue or a TypeScript regression issue. But both these issues seem tied to recent updates related to TS intersection types and I am leaning somewhat to this being a TypeScript optimisation problem (where TypeBox is hitting some recursion limit)
I have been able to narrow down the issue however; it relates to how TypeBox decomposes object properties for their optional
and readonly
modifiers which are then recomposed via an intersection. TypeBox provides 3 modifier types ...Type.Optional(..)
, Type.Readonly(..)
and Type.ReadonlyOptional(..)
.
I can push a fix which resolves TS2589, however it does involve removing the Type.ReadonlyOptional(...)
modifier. Offending code below.
// Extract 'optional', 'readonly' and 'default' property keys from T
type ReadonlyOptionalPropertyKeys<T> = { [K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? K : never }[keyof T]
type ReadonlyPropertyKeys<T> = { [K in keyof T]: T[K] extends TReadonly<infer U> ? K : never }[keyof T]
type OptionalPropertyKeys<T> = { [K in keyof T]: T[K] extends TOptional<infer U> ? K : never }[keyof T]
type PropertyKeys<T> = keyof Omit<T, OptionalPropertyKeys<T> | ReadonlyPropertyKeys<T>>
type StaticObjectProperties<T> =
// commenting the line below resolves TS2589
// { readonly [K in ReadonlyOptionalPropertyKeys<T>]?: Static<T[K]> } &
{ readonly [K in ReadonlyPropertyKeys<T>]: Static<T[K]> } &
{ [K in OptionalPropertyKeys<T>]?: Static<T[K]> } &
{ [K in PropertyKeys<T>]: Static<T[K]> }
At this stage, I'm unable to produce a trivial repro for the issue to submit to the TS team, and I am a bit uneasy about hastily changing the API (potentially removing Type.ReadonlyOptional(..)
) if the issue actually rests with TypeScript.
Give me a day or so to have a think about next steps. Any feedback or insights into the TS 3.9 update happening elsewhere in the community would be helpful in understanding the extent of the problem. In the interim, and if possible, I would recommend keeping with TS 3.8.
from typebox.
Have decided to just drop Type.ReadonlyOptional(..)
which should resolve that unusual TS2589 error. I have done a minor semver revision, bumping TypeBox to 0.10.0
.
Are you able to confirm things are fine yourside? If so, would be good to close off this issue.
from typebox.
Hi @sinclairzx81,
On TS 3.9.3, using your latest version, TS2315 & TS2589 are no more triggered.
I can use again buggyConvertFoos
too.
I've removed my Enums definitions 'cause I don't need them anymore, so I didn't check if there is still issue about it or not.
from typebox.
@Gounlaf Alright cool, thanks for checking your side, will close this one off.
Cheers
S
from typebox.
nights I come for a similar good topic in another library and I discovered that if we export the interface with export type { } it works
from typebox.
Related Issues (20)
- Casting `uniqueItems` array with object items HOT 2
- Type instantiation is excessively deep and possibly infinite .ts(2589) when using Type.Exclude HOT 2
- JsonTypeBuilder#Record not passing on options HOT 1
- No implicit defaults on Value.Create() HOT 5
- [BUG] Value.create caches the default date HOT 1
- Feature request: Value.Assert and TypeCheck.Assert HOT 3
- [Feature Request?] allow more flexible input for string pattern HOT 3
- Individual types imports naming conflict with native classes HOT 4
- Strict error: Unknown type HOT 2
- Maximum call stack size exceeded. HOT 2
- TUnion of literals from const array HOT 2
- "Value.Clean" doesn't work with Unions of objects with prohibited additionalProperties HOT 3
- JS floating point precision bug causes multipleOf validation to fail HOT 4
- Tuple with rest items (or Array with prefixedItems) HOT 2
- useDefaults HOT 3
- Support for discriminator HOT 1
- Support for self-referencing fields of recursive type HOT 2
- Property '[Kind]' is missing in type 'TObject' but required in type 'TObject<TProperties>' HOT 3
- [feat] Bail / Early Cancel Mode HOT 1
- Setting Description HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typebox.