IssueRaiser

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

Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments

github (Other)

Suggestion for Dependent-Type-Like Functions: Conservative Narrowing of Generic Indexed Access Result Type

rubenpietersAug 21, 2019, 5:51 PM

# 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.

Comment on this issue