-
Notifications
You must be signed in to change notification settings - Fork 12.8k
feat(auto): reference own type in type declaration #49120
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
For the record:
|
Fair enough, it still applies to the other reasons I've mentionned though. On top of that, writing // can be confusing and still brings up the concern of having
// long type names
let foo: Awaited<ReturnType<typeof bar>> | null = await bar();
// vs
let foo: auto | null = await bar(); |
Duplicate of #36450. |
Not an exact duplicate of #36450 - that issue just wants an explicit keyword for type inference (i.e. not a new feature), this asks to be able to combine the auto-inferred type with other types via union, which AFAIK isnβt currently possible without writing a constrained identity function. |
That's true, but I see no way to support this feature without such a keyword and without adding some really weird special syntax for a corner-case. And I've just realized that this is valid syntax: While less nice, you can get the desired feature by using a function: function nullify<T>(val: T): T | null { return val; }
const foo = nullify(await bar()); |
Indeed, |
On that note, this is probably similar in spirit to what #47920 aims to enable. |
This is super fraught since the contextual type of a variable's initializer is the type annotation itself. Ex: declare function fn<T>(cb: (arg: T) => void): T;
// s: string
const a: string = fn(s => s); If you wrote const a: auto | string = fn(s => s); then I'm not even sure what should happen, and you could probably construct "unsolvable" setups with this. Another potential solution is explicit type inference (#26242), where you could (presumably?) write type Nullable<T> = T | null;
const m: Nullable<infer> = expr; FWIW I don't think The use case here makes way more sense than #36450 so I'll refrain from duping to that. |
Huh, I would have expected
|
Suggestion
Similarly to C++, having an auto type that would infer the type of a variable and that would be usable in its type declaration.
π Search Terms
auto in type declaration
β Viability Checklist
β Suggestion
Currently, there isn't a really neat and simple way of catching the type of a variable in its type declaration, which can be quite annoying when we want that variable to have other allowed types alongside with its original type, without needing to declare it as
any
or typing explicitely its type.ππ» Example and use cases
Let's say you're working with a library that deals with a lot of types

In this example I'm using mongoose, and as you can see, certain type declarations don't even fit on my screen!
Now, let's say that you want to call one of the methods in this screenshot and register its value in a variable, but allow another type to go along with it, like for eg. a string, or to allow it to also be null?
The only 3 ways you could do this right now in typescript is to either;
any
type, which isn't really safebut which wouldn't even work out of the box because in this example the method we're calling is async and we're awaiting it, which returns the non promised type but... ReturnType gives us back a promiseOh and this example is only for when dealing with functions, what would you do if you were dealing with an object? You can't use ReturnType so you'd have to either copy it's type name or define it as
any
On top of that, writing
Awaited<ReturnType<typeof fn>>
is much more longer than just using auto, and readability and clarity take a hitIntroducing auto would allow us to catch the type name of our variable and be able to mess with its type declaration
The text was updated successfully, but these errors were encountered: