-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Mapped conditional type's type information not used to constrain type of indexed access #42882
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
Comments
|
type Observer = {
s(s: string): void;
n(n: number): void;
}
const p = new Observable(null as any as Observer);
const key: keyof Observer = Math.random() > 0.5 ? "s" : "n";
// Passes a number to a string function half the time
p.observeShouldWork(key, 42); See also #27808 |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Thanks for taking a look. I was aware of the fact that F only upper-bounds the method name, which is why included the following code lines (which demonstrate the unsoundness problem you point out) in the TypeScript playground code I attached: // The following is working as expected:
test.observeShouldWork<"foo" | "bar">("foo", true); However, my bug report does not pertain to that problem: My bug report is about the failure of the type system to derive that I just tried 4.3.0-dev.20210219 and the problem seems to persist. I also saw that Generalized Index Signatures are on the iteration plan for 4.3. Maybe this problem can be solved as part of that work? |
What you get here is a type that's assignable to You can alias it to const f: Function = this.observer[methodName];
// OK
f.apply(this.observer, args) |
Thanks for the clarification Ryan, your suggestion solved the problem. I thought that deferred lookup types were an implementation detail; the error message (Property 'apply' does not exist on type 'Observer[F]'.(2339)) also doesn't point to the problem (or how to solve it, for that matter). |
Bug Report
Hi, I have a case where I'm unsure whether I'm holding things wrong, or whether this is a bug, or whether this is the expected behavior.
🔎 Search Terms
Generics, type inference, type checking, type constraints
🕗 Version & Regression Information
4.2-dev, no regression range information. This is the behavior in every version I tried, and I reviewed the FAQ for entries about apply and functions.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
TS complains that property 'apply' does not exist on type 'Observer[F]'.(2339) in
f.apply
above.🙂 Expected behavior
Since F is constrained to
NamedFunctionProperties<Observer>
, and TS does derive the typeObserver[F]
forf
(that isthis.observer[methodName]
), I would have expected that this is sufficient to know thatf extends CallableFunction
. Maybe I'm missing something.Thanks for taking the time to look at this.
The text was updated successfully, but these errors were encountered: