Mechanism for late inferred types in control flow analysis #60907
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
π Search Terms
"control flow analysis" with variations of array, widening, callbacks, etc
β Viability Checklist
β Suggestion
I have an issue where I want to capture a type defined in a scope that is inaccessible from my current scope. For example, I want to create a wrapper for some OpenAI function.
Playground
As you can see, the goal is to have an auto-widening type based on usage later on (within the same file). This somewhat already works with control flow analysis for arrays, as you can see in the
type Good = ...
type above.Realistically, when this situation occurs, application authors will type
onMessageResponse
as(message: any) => void
, which is unfortunate because all the type information is there and just needs a way to be bubbled up to the higher scope.It would be nice if there was an escape hatch to tell the type system that I care about the typeof a value declared somewhere inaccessible to the current type scope within the file. Obviously, this wouldn't actually use an untyped array with some annotation on the type; this would probably be a special new named type like LateInferredType or something similar, e.g.:
Or perhaps there's a non-breaking way of declaring this syntax that I'm not thinking of.
π Motivating Example
This allows smarter inference without needing to chase down every derived property. Sometime the specific types aren't even exported by the upstream libraries and there's no good way to mention them without recreating a large complex type which is fragile and error prone.
π» Use Cases
This is useful for writing type safe code where you want to expose a complex type that doesn't have a concrete definition and is more tied to the specific shape of a single step in a complex application
There's no way to signal later in a program or without a callback that a type should be widened, but sometimes that exact functionality is needed
None
The text was updated successfully, but these errors were encountered: