gleam-lang/gleam

Improve TS type narrowing

Open

#5 715 ouverte le 13 mai 2026

Voir sur GitHub
 (5 commentaires) (1 réaction) (0 assignés)Rust (960 forks)batch import
help wanted

Métriques du dépôt

Stars
 (21 417 stars)
Métriques de merge PR
 (Merge moyen 10j 19h) (69 PRs mergées en 30 j)

Description

In the following issue, the example are made with a simple Option(a) in Gleam.

When using an Option(a) to TS, we know for sure that the expected type is Option$, so we can type functions signatures as (value: $option.Option$<EL>) => ..... When using the new API, "pattern matching" is defined as $option.Option$isSome(value)). The type narrowing works, but will refine value from Option$<EL> to Option$<unknown>, because isSome is defined as export function Option$isSome<EL>(value: any): value is Option$<unknown>;.

Thanks to TS and function overloading, we could do way better, and keep the generic type, while narrowing to the correct type. We could write an overload export function Option$isSome<EL>(value: Option$<EL>): value is Some<EL>. That way, it would keep the generic.

Guide contributeur