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
// See https://github.com/Microsoft/TypeScript/issues/7556.typeErrorT={new(message: string): Error};typeEither={tag: "Left";a: string}|{tag: "Right";b: number};// --- Explicit version (assertion type information is propagated):functionassertExplicit(condition: boolean): asserts condition{if(!condition){thrownewError("oops");}}functiontestWorks(x: Either){assertExplicit(x.tag==="Right");console.log(x.b);}// --- Partial application version (assertion type information is lost):function_assert(errorType: ErrorT,condition: boolean): asserts condition{if(!condition){thrownewerrorType("oops");}}constassert=(condition: boolean)=>_assert(Error,condition);functiontestFails(x: Either){assert(x.tag==="Left");console.log(x.a);// fails with "Property 'a' does not exist on type 'Either'."}export{};
🙁 Actual behavior
Type assertion information is lost outside of immediate call-sites. For example, if f(x): asserts <condition> then TS will only respect the assertion of the condition in the direct scope of where f is called and nowhere else, eg if g call f, there is no propagation of the information that asserts <condition> should hold in g.
🙂 Expected behavior
Assertion types to be propagated everywhere they apply.
The text was updated successfully, but these errors were encountered:
But why is assert not an asserting function? Simple control-flow analysis should reveal that it ought to have the same asserting properties as _assert since there's no try/catch in assert.
Bug Report
🔎 Search Terms
type assertion, assert v is T, asserts condition, partial application, type inference, case splitting
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Type assertion information is lost outside of immediate call-sites. For example, if
f(x): asserts <condition>
then TS will only respect the assertion of the condition in the direct scope of wheref
is called and nowhere else, eg ifg
callf
, there is no propagation of the information thatasserts <condition>
should hold ing
.🙂 Expected behavior
Assertion types to be propagated everywhere they apply.
The text was updated successfully, but these errors were encountered: