You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is the behavior in every version I tried, and I reviewed the FAQ for entries about "narrow", "dependent". I also looked through all the headings and didn't see any relevant sections.
typeNumericKind="int"|"float"|"fixed";typeKindToTsType<KextendsNumericKind>=Kextends("int"|"float") ? number : string;typeExpression<R>=ExpressionAbsUnion<R>;exporttypeExpressionAbsUnion<R>={[KinNumericKind]: ExpressionUnary<R,"abs",K>;}[NumericKind];exporttypeExpressionUnary<R,Kextendsstring,FextendsNumericKind>={kind: K;phantom: R[];resultKind: F;operand: Expression<R>&{resultKind: F};}exportfunctionevaluate2<R,EextendsExpression<R>>(expr: E,root: R): KindToTsType<E["resultKind"]>{if(expr.kind=="abs"&&expr.resultKind=="int"){letx: KindToTsType<typeofexpr.resultKind>;// x is declared correctly as numberconstoperand=evaluate2(expr.operand,root);// operand is declared correctly as number// ----------------------------------------------// ------------ THE ERROR IS HERE: ------------// ----------------------------------------------// The return value of type number is incorrectly rejected without type assertion:returnMath.abs(operand)asKindToTsType<E["resultKind"]>;}thrownewError("Not implemented");}
🙁 Actual behavior
The declared return type of KindToTsType<E["resultKind"]> and the actual return type of number in the narrowed branch are not compatible.
🙂 Expected behavior
E should be proven to be ExpressionUnary<R, "abs", "int"> in the branch and therefore the compiler should know that KindToTsType<E["resultKind"]> is equal to number
Additional information about the issue
The text was updated successfully, but these errors were encountered:
🔎 Search Terms
"Type narrowing", "generic dependent return type", "GADT", "GADT expression evaluation"
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about "narrow", "dependent". I also looked through all the headings and didn't see any relevant sections.
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250405#code/C4TwDgpgBAcgrgWwgJwJYGMDSqB2ATKAXigCJdgSoAfUgMwBsB7AQwurtQA8I8SBuAFChIUbPgAqjcQGdx4CAB5MUCJ2AR802IhQYxeAHxFRKtRrxaAFGRx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KAA
💻 Code
🙁 Actual behavior
The declared return type of
KindToTsType<E["resultKind"]>
and the actual return type ofnumber
in the narrowed branch are not compatible.🙂 Expected behavior
E
should be proven to beExpressionUnary<R, "abs", "int">
in the branch and therefore the compiler should know thatKindToTsType<E["resultKind"]>
is equal tonumber
Additional information about the issue
The text was updated successfully, but these errors were encountered: