Suggestion for Dependent-Type-Like Functions: Conservative Narrowing of Generic Indexed Access Result Type
# Search Terms generic indexed access type dependent function generic narrowing # Suggestion TypeScript does not provide dependent types, but by combining various features such as unions, literal types and indexed access types we can obtain fairly similar behaviour to a dependent function from the caller point of view. Unfortunately, the implementer of such functions is faced with either the possibility of creating unsoundness (pre 3.5) or forced to assert the return type with unsound features (post 3.5). There are some differences with typical dependent functions, which is why we call these _dependent-type-like_ functions. This suggestion proposes an addition to the typechecking part of TypeScript, which aids the programmer in writing dependent-type-like functions. The main attractive point of this suggestion is that it does not introduce any additional syntax, and is meant to be a conservative extension. Thus it is not a breaking change with regards to the changes introduced in 3.5, but it does enable certain sound scenarios to typecheck. # Use Case The main use case is dependent-type-like functions, the `depLikeFun` example is a minimalistic scenario showcasing such a situation. In this example, we have the `depLikeFun` function which returns `number` when its input is of type `"t"` and `boolean` when its input is of type `"f"`. ```ts interface F { "t": number, "f": boolean, } function depLikeFun<T extends "t" | "f">(str: T): F[T] { if (str === "t") { ret
Posted by rubenpieters on Aug 21, 2019, 5:51 PM
Tags
Related issues
- struct's lifetime requirements can be bypassed via covariant field assignment
- Proposal to Reorganize the Moderation Team and Review Moderation Policies
- Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
- proposal: container/...: generic collection types
- Better Custom Endpoint BYOK
- A concrete policy for backward compatibility until Godot 5.0
Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments
github (Other)
struct's lifetime requirements can be bypassed via covariant field assignment
Proposal to Reorganize the Moderation Team and Review Moderation Policies
Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
proposal: container/...: generic collection types
Better Custom Endpoint BYOK
A concrete policy for backward compatibility until Godot 5.0
New MTP models not supported
Tracking: 429 / Capacity Issues
Suggestion for Dependent-Type-Like Functions: Conservative Narrowing of Generic Indexed Access Result Type
# Search Terms generic indexed access type dependent function generic narrowing # Suggestion TypeScript does not provide dependent types, but by combining various features such as unions, literal types and indexed access types we can obtain fairly similar behaviour to a dependent function from the caller point of view. Unfortunately, the implementer of such functions is faced with either the possibility of creating unsoundness (pre 3.5) or forced to assert the return type with unsound features (post 3.5). There are some differences with typical dependent functions, which is why we call these _dependent-type-like_ functions. This suggestion proposes an addition to the typechecking part of TypeScript, which aids the programmer in writing dependent-type-like functions. The main attractive point of this suggestion is that it does not introduce any additional syntax, and is meant to be a conservative extension. Thus it is not a breaking change with regards to the changes introduced in 3.5, but it does enable certain sound scenarios to typecheck. # Use Case The main use case is dependent-type-like functions, the `depLikeFun` example is a minimalistic scenario showcasing such a situation. In this example, we have the `depLikeFun` function which returns `number` when its input is of type `"t"` and `boolean` when its input is of type `"f"`. ```ts interface F { "t": number, "f": boolean, } function depLikeFun<T extends "t" | "f">(str: T): F[T] { if (str === "t") { ret
Comments (0)
No comments yet. Start the discussion.