-
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
Compiler inconsistently (but successfully?) resolves a type which indirectly references itself #43683
Comments
I'm mostly interested in knowing if the double I'll try building TypeScript locally and seeing what ways there are for stepping through what's happening... |
The compiler tends to resolve types as lazily as possible, and adding an extra function between a type and one of its members whose type is ultimately recursive could certainly defer work which, if done earlier, would have been a circularity. To answer your question, I think this is likely to continue working, but not outside the realm of possibility of breaking. I would certainly never tell someone “Please do |
Thanks for the response Andrew. I didn't realize you could add a return type on arrow functions that are in objects, that's a great answer. In the playground I linked I didn't do that and was unhappy with how verbose the alternatives were. Your version is certainly terse enough, so I'll suggest that design. I imagine the lazy evaluation isn't a bug, even though it's a bit confusing. |
The reason it was erroring here was the constraint; it was trying to check the |
That's amazing thanks! I've poked around the playground a bit and I feel like TS is at least a lot more consistent now with @tjjfvi how were you able to see that the evaluation was defered? Is that something people need to run TS in a debugger for? I'd love to see how TS breaks down its error reporting to avoid this in the future. |
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
Tested and occurs in TS Playground on 4.3.0-beta, 4.2.3, 4.1.5, 4.0.5.
⏯ Playground Link
Playground link with different test cases/attempted workarounds
💻 Code
I'm trying to port a vanilla JS reactive state library to TS. Signals are just getter/setter functions that store a value (
type Signal<T> = { (): T }
). Defining them with a function produces a signal that has a dynamic value.This is code that a user would write (not the
declare
bit obviously; playground has full example):Instead I tried using
() =>
to add a level of indirection. This surprisingly works, but I don't think it should...This works. I worry, is this an inconsistent resolution by the TS compiler? It isn't capable of resolving the shorter first example, but it can do the second example which uses
infer R
in the same way.I'd like to use the first example, since it feels very odd to tell users "Please do
() => () => X
because... TS is like that"🙁 Actual behavior
TS is able to resolve a more complicated variable type that more deeply references its own initializer, but not a more simpler/shallower reference.
🙂 Expected behavior
TS would be consistent in applying the 7022 error, either resolving the first example, or failing the second example.
The text was updated successfully, but these errors were encountered: