Purpose and safety of Any/Compute #177
-
I'm looking at the However, I think that I lack the deep TS expertise to know why that's the case. With that in mind:
Thanks for a great library! 🙂 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I assume that this is purely for efficiency yes, or so that we can abstract certain types away (that we don't want to show), or both at the same time. It is completely safe, there is absolutely no type loss. However, there is a drawback at the moment: type O = {
a: 'a'
o: O
} & {
b: {
o: O
}
}
type ComputedO = Compute<O> Will yield a type that, when you look at it, is filled with type test0 = ComputedO['o']['o']['o']['a'] This happens only on circular references. Lucky for us, I'm going to roll out a version that is able to handle circular references - to have even more beautiful outputs. If you want, give it a shot: import {M} from 'ts-toolbelt'
export type ComputeDeep<A extends any, Seen = never> =
A extends object
? A extends M.BuiltInObject | Seen
? A
: {
[K in keyof A]: ComputeDeep<A[K], A | Seen>
} & {}
: A
type A = {
a: string
o: O
}
type O = {
a: 'a'
o: A
}
type ComputedO = ComputeDeep<O>
type test0 = ComputedO['o']['o']['o']['a'] The TypeScript team provide very generic tools to do many things with the type system, they give us the choice to build libraries - which is great. But this can make it hard to learn and his is what
Thanks! If you have any question, I'm always here to help! |
Beta Was this translation helpful? Give feedback.
I assume that this is purely for efficiency yes, or so that we can abstract certain types away (that we don't want to show), or both at the same time.
It is completely safe, there is absolutely no type loss. However, there is a drawback at the moment:
Will yield a type that, when you look at it, is filled with
any
s. It just looks likeany
. In fact the type information is well preserved:This happens only on circular references. Lucky for us, I'm going to roll out a version that is able to handle circular references - to have even more beautiful outputs…