Comments (7)
from typescript.
The first one doesn't really infer F
. It only looks like it got inferred because that's the only kind you have in URItoKind
. What you might see as "inferred" there is just the constraint.
The second one is different because at the time that you get to inferring A
and B
TypeScript has already settled on what F
is (just based on the constraint). In there, the signature that goes through inference is <A, B>(f: (a: A) => Maybe<B>) => Maybe<B>
. You can see how the return type of that callback is already instantiated to Maybe<B>
and it's not a deferred generic indexed access (Kind<F, B>
) like in the first one.
from typescript.
You can add readonly ['Array']: Array<A>;
into URItoKind
and it still works the same way.
from typescript.
Yes, you can see now how the first function doesn't infer "Maybe"
for F
here either. My point is that those two examples are very different. One deals with a deferred generic indexed access. The compiler is still trying to figure out what F
could be (and it even fails to do it altogether). The other one doesn't suffer from this problem because F
is already instantiated in a sense. So this time the compiler infers into a union type.
Your post implied that both are largely the same but they are not. I'm not saying that it's impossible to improve this - that I don't know ;p but those two situations deal with a different level of complexity. My educated guess is that if it would have to be improved it would have to be done here
from typescript.
Thank you for your explanation. I guess my question is, if this is an expected behavior or a bug, and if there is anything I can do right now to make test1
work.
from typescript.
While it would be nice for this to work, we'd need #30134 to make this work in the general case where one type parameter depends on the resolution of the previous one. The current algorithm isn't well equipped to insert an extra round of inference here to settle on F
before trying to figure out B
, and it's not super apparent to me that it'd be tractable to do so (consider the case where URItoKind
is very large).
The example is a bit under-specified to give more advice. As written it seems kind of ambiguous, e.g. test2
works but gives different answers depending on the order of Maybe1
and Maybe2
.
interface URItoKind<A> {
readonly ['Maybe1']: Maybe<[A, unknown]>;
readonly ['Maybe2']: Maybe<[unknown, A]>;
}
type URIS = keyof URItoKind<any>;
type Kind<URI extends URIS, A> = URItoKind<A>[URI];
class Maybe<A> {
_inferHelper!: A;
static of: <A>(a: A) => Maybe<A>;
}
declare function test1<F extends URIS, A, B>(f: (a: A) => Kind<F, B>): [F, B, Maybe<B>];
declare function test2<F extends URIS>(): <A, B>(f: (a: A) => Kind<F, B>) => [F, B, Maybe<B>];
declare const func: (x: number) => Maybe<[string, number]>;
const t1 = test1(func); // Maybe<unknown> - Doesn't work
const t2 = test2()(func)[2];
from typescript.
This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
from typescript.
Related Issues (20)
- Allow type annotations in .js files in preparation for Type Annotations proposal
- Variables (extracted from a discriminated union object) lose narrowed types after being exported HOT 2
- Number.prototype.toFixed, Number.prototype.toExponential,Number.prototype.toPrecision comments error HOT 3
- Newly introduced type narrowing via discriminators on .filter does not work in this case HOT 9
- Long running encodedSemanticClassifications-full request HOT 3
- 5.6.0 regression: Incorrect param type inference for type with all optional props HOT 6
- Make type narrowing for destructured discriminated unions work for more types HOT 1
- Design Meeting Notes, 8/16/2024 HOT 3
- Inheriting from 21 combined mixins causes "type instantiation is excessively deep and possibly infinite" HOT 5
- `void` is treated as covariant with everything when overriding a method from a base class HOT 2
- Type Instantiation is Excessively Deep Regression In #56004
- Signatures with less parameters aren't assignable to compatible targets with more when their rest param is an instantiated `NoInfer`
- Enable JSX.Element to be checked based on the element type HOT 2
- [NewErrors] 5.6.0-dev.20240818 vs 5.5.4 HOT 73
- Add `--strictObjectIterables` (bikeshed): exclude primitive (`string`) from `Iterable` types HOT 3
- [ServerErrors][JavaScript] 5.6.0-dev.20240818 vs 5.5.4 HOT 7
- [ServerErrors][TypeScript] 5.6.0-dev.20240818 vs 5.5.4 HOT 15
- `getTextOfJSDocComment` introduces a space in JSDoc comments HOT 1
- [tsserver] Make "configure excludes" warning more debuggable HOT 1
- Module resolution: Named imports not supported with import attributes 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 typescript.