-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Circularities Only Blocked Sometimes #37426
Comments
Sorry if I hijack this issue, but I truly am not sure whether what I'm also experiencing somehow has a similar root cause to the OP (similar issue search took me here). @harrysolovay perhaps you could see if the behavior described below contributes to/is related to your issue in any way? The issue that I've come across is that if a generic interface ( interface GenericBox<T> {
value: T;
}
type Foo = GenericBox<Foo> | string; // OK
type Bar<T> = GenericBox<T>;
type UnionOfBar = Bar<UnionOfBar> | string; // Circular reference error |
@soul-codes circularities are allowed in tuples, of which Also––if you wish to ask––please do so on StackOverflow, and link to your question from here. This forum is really only for bugs/potential bugs/feature requests. |
This might be fixed by #37423 ; it's worth checking. If not, this is kind of like how the police only sometimes pull you over for speeding. "Fixing" this probably won't be in the direction you want 😉 If you can narrow this down to a super simple example we could advise further |
@harrysolovay understood. I just wondered if our issues were in fact the same and perhaps, as Ryan has requested, yours could simplify to mine. It seems like it is rather a misunderstanding of how circularity is supposed to work on my side which led me to mistake it as a bug/issue. I will proceed to SO accordingly. Sorry to pollute this issue folks! |
I was definitely speeding 😂 I managed to implement the desired type-mapping thanks to a StackOverflow answer provided by the creator of Punchcard, a serverless DX, which I encourage all to check out! One of the monorepo's packages, "Shapes" (like "Codecs"), is used to map between the representations of different services while enforcing type-safety. Really, really cool. Anyone who's trying to achieve recursive mapping between types should check it out. |
TypeScript Version: 3.8.2
Search Terms: circular, type, mapping, tuple, corresponding, signature
This builds off of the issue I just submitted (tagged as a bug).
In summary, I'm trying to create a virtual type system.
I define the available types:
I create a
Codec
type:And I create a utility type, which can be used to unwrap / gather the corresponding type.
Surely enough, it works for
Codec<Type.Int>
:It works for
Codec<Type.Boolean>
:And it works for
Codec<Type.List>
:TypeScript Playground of the example up to this point
We see
Decode
works, even though it's self-referencing. There's no cycle-related error––presumably because of the use of tuples, which seem somewhat cycle-friendly since 3.7. Now let's add another type:enum Type { Boolean = "Boolean", Int = "Int", List = "List", + Union = "Union", }
We modify the
Codec
type to support multiple array of typeCodec
(the types to unite):And we update the definition of
AnyCodec
:Let's instantiate this type:
And let's create and use the corresponding
DecodeUnion
utility:TypeScript Playground of this example, continued up to this point
The utility works!
UnionOfIntAndBooleanDecoded
is inferred as being of typenumber | boolean
. Last but not least, let's integrateDecodeUnion
into the more generalDecode
utility type.This is where we run into trouble:
TypeScript Playground, with the error-producing code
While the prior self-reference did not result in a circularity error, this one does:
Type alias 'Decode' circularly references itself
.Is there a workaround? Could this be related to the aforementioned issue?
I know I've said it many times, but I truly mean it every time when I say: your help is greatly appreciated & thank you!!!
The text was updated successfully, but these errors were encountered: