Proposal: Get the type of any expression with typeof
Problem
Working Implementation for this Proposal Try it out: `npm install yortus-typescript-typeof` View the diff: here. Problem Scenario TypeScript's type inference covers most cases very well. However there remain some situations where there is no obvious way to reference an anonymous type, even though the compiler is able to infer it. Some examples: I have a strongly-typed collection but the element type is anonymous/unknown, how can I reference the element type? (#3749) [code block] A function returns a local/anonymous/inaccessible type, how can I reference this return type? (#4233, #6179, #6239) [code block] I have an interface with a complex anonymous shape, how can I refer to the types of its properties and sub-properties? (#4555, #4640) [code block] Why do we need to Reference Anonymous/Inferred Types? One example is declaring a function that takes an anonymous type as a parameter. We need to reference the type somehow in the parameter's type annotation, otherwise the parameter will have to be typed as `any`. Current Workarounds Declare a dummy variable with an initializer that infers the desired type without evaluating the expression (this is important because we don't want runtime side-effects, just type inference). For example: [code block] This workaround has a few drawbacks: - very clearly a kludge, unclear for readers - identifier pollution (must introduce a variable like `dummyReturnValue`) - does't work in ambient contexts, because it requires an imperative
Error Output
ERROR // A strongly-typed collection with special indexer syntax. How to reference the element type?
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: Proposal: Get the type of any expression with typeof
> type P = typeof foo(0); // P is any[] > type Q = typeof foo(true); // Q is string I think using types as argument instead of values is a more valid syntax. [code block] It is more clear that the function is not being called, because you provide types and not values as arguments. The other point, is it is less ambiguous. Some people will use `typeof foo(false)`, whereas some people will us
Trust Score
7 verifications
- 1
> type P = typeof foo(0); // P is any[]
> type Q = typeof foo(true); // Q is string
- 2
I think using types as argument instead of values is a more valid syntax.
I think using types as argument instead of values is a more valid syntax.
- 3
It is more clear that the function is not being called, because you provide type
It is more clear that the function is not being called, because you provide types and not values as arguments. The other point, is it is less ambiguous. Some people will use `typeof foo(false)`, whereas some people will use `typeof foo(true)`. If you have types as arguments people can only write `typeof foo(boolean)`.
Validation
Resolved in microsoft/TypeScript GitHub issue #6606. Community reactions: 43 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep