GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||
GeezerVison is showing github.com — Instantiation expressions by ahejlsberg · Pull Request #47607 · microsoft/TypeScript Conversation
|
|
Anders Hejlsberg (@ahejlsberg) This is super close to a solution of #37181. Will it will allow us to do: function makeBox<T>(value: T) { return { value }; }; // Can we do it or something similar? Currently doesn't compile :( type MakeBox<T> = ReturnType<typeof makeBox<T>> // As it now allows us to do (no generics though) const stringMakeBox = makeBox<string>; type MakeBox = ReturnType<typeof stringMakeBox> // And even more relevant to support generics if we can now do: type MakeBox = ReturnType<typeof makeBox<string>> Up to now |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Building the TypeScript playground for this PR would be better. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Yes, you can indeed use that pattern to capture a generic return type of a generic function. This is something that previously wasn't possible. I'll update the PR description to include an example. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Let's say we have a function f have a call signature What will this PR behave? They have different argument counts and even different different type constraints |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
This works just fine: declare const f: { <T extends number, Q>(): [T, Q]; new <T extends string>(): [T]; }; type T0 = typeof f<string>; // new () = [string] type T1 = typeof f<number, string>; // () => [number, string] But an error occurs here: declare const g: { <T extends number>(): [T]; new <T extends string>(): [T]; }; type T2 = typeof g<string>; // Error, type 'string' does not satisfy the constraint 'number' |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
TypeScript Bot (@typescript-bot) test this |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Heya Anders Hejlsberg (@ahejlsberg), I'm starting to run the inline community code test suite on this PR at 8e466ba. Hold tight - I'll update this comment with the log link once the build has been queued. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Did this feature get released in 4.7? Somehow when I try the example with ts version |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Yep, works in the playground - given that those inline error messages dont come from the official typescript extension (to my knowledge)- it's likely a local setup issue causing this |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
It doesn't works on build-ins method 🤔 const cache = new WeakMap< A, ReturnType<typeof Object["getOwnPropertyDescriptors"]<A>> // error >(); const a = { b: 1 } type A = typeof a const descMap = Object.getOwnPropertyDescriptors<A>(a) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
You cant use brackets, just qualified names: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
How to use this feature with overloaded functions ? declare function foo<T extends keyof HTMLElementTagNameMap>(arg: T): HTMLElementTagNameMap[T]; declare function foo<T extends HTMLElement>(arg: T): T; type FirstFoo = typeof foo<"div">; // error const f: ReturnType<FirstFoo> = foo("div"); I couldn't get the code to work. The error message is The code works if I change the second |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Higher-order instantiation expressions don't resolve properly, as they weren't accounted for in the test baselines (see also issue #52035). declare function transform< const A extends readonly any[], F extends < V extends A[number], K extends keyof A, >(value: V, index: K, array: A) => any >(array: [...A], func: F): { [K in keyof A]: ReturnType<typeof func<A[K], K>> }; const $2 = transform(["object", "string", "number"], k => ({ type: k })); // ^? // Should be [{ type: "object" }, { type: "string" }, { type: "number" }], but is [any, any, any] |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Is there any issue tracking a design for a way we could do that? I've been trying to do something that I'd be able to accomplish if I could have a type like // type ApplyFn<Fn, Arg> = (some magic...) type Applied = ApplyFn<<T>(value: T) => { value: T }, 1 | 2 | 3> // { value: 1 | 2 | 3 } Someone correct me if I'm wrong but it seems there's no way to do this for an arbitrary function. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
It also solves #20719. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I don't believe there is. Instantiation expressions only work for values; you can't use a similar method in types. type Fn = <Arg>() => unknown type ApplyFn<T, F extends Fn> = T extends unknown ? ReturnType<F<T>> : never const f = <T>(): [T] => ({} as [T]) type t1 = ApplyFn<1 | 2, typeof f> The following (direct instead of the type parameter) works: type ApplyFn<T> = T extends unknown ? ReturnType<typeof f<T>> : never type t1 = ApplyFn<1 | 2> // ^? [1] | [2] |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Apply function generics as part of type definition Allow binding generic functions to a given type
There was an error while loading. Please reload this page.