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
Search Terms: assertion condition, narrow assertion condition, assertion condition union
Code
typeMyUnion={[key: string]: any}|string[];functionassertObject(value: any): asserts value is Record<string,any>{if(value===null||typeofvalue!=="object"||Array.isArray(value)){thrownewError("Assertion failed");}}functiongetValue(): MyUnion{return{foo: "bar"};}constvalue=getValue();assertObject(value);// Error as it does not know if it is a string[] or { [key: string]: any }console.log(value["propName"]);
I've also observed that TypeScript is using the already known type at caller (i.e. MyType) to narrow after the assert function is called. This is resulting in never as a result.
I am not sure this part is behaving as expected, but seems to be related to the bug reported.
typeMyType=|{[key: string]: {[k: string]: number;};}|string;consta: MyType={foo: {bar: 123}};functionassertUndefined(value: any): asserts value is undefined{if(value!==undefined)thrownewError();}assertUndefined(a.foo);// `a.foo` is `never`console.log(a.foo);
Expected behavior: assert condition should narrow the type to one of the values in the union.
Actual behavior: it seems to be merging what is knows about the type at the caller with what the assertion returns.
Thanks, @RyanCavanaugh, for your answer and your time. I was suspecting this could be the case, but I did the lazy thing and brought the issue here. Nevertheless, now you can't have the key push, right?
But how about the second case I've added? Does TypeScript strictly follow the assertion or it merges with the type information it has on the caller? I am not sure this is my fault, but I don't recall finding deeper details on the documentation.
TypeScript Version: 3.9.0--dev.20200319
Search Terms: assertion condition, narrow assertion condition, assertion condition union
Code
I've also observed that TypeScript is using the already known type at caller (i.e.
MyType
) to narrow after the assert function is called. This is resulting innever
as a result.I am not sure this part is behaving as expected, but seems to be related to the bug reported.
Expected behavior: assert condition should narrow the type to one of the values in the union.
Actual behavior: it seems to be merging what is knows about the type at the caller with what the assertion returns.
Playground Link: https://www.typescriptlang.org/v2/en/play?ts=next#code/C4TwDgpgBAsiCqA7Alge0VAvFA3lA2gNYQgBcUAzsAE7KIDmAuuQIaIhQC+UAPpTXXr5GAbgBQYgGYBXRAGNgaDCwoUI1YAHkARgCsICgBQA3FgBtpEVuwCUrVeuAUopi9GTOASgdTUAJgA8VLQMADRQbCAAfLhiUPFQyJJQJuaWWJjYiNJmZrx8oJCoya7pAISZUABEqHoGwFX5UACC1NQsIAB0Hq3tIKluNjaxCaNQwAAW1KgA7lCIEHMAom2+hlXNDhpKUJIsyGYQflU24qOcYhdSsgo79BDAAGppEIZ2sAgo6CMJ1A-S1AweEkqFQ5Cq2hY1EanHEVzk6CoLheWCg9yeLze4jEKjUGh0+iMpQgpwkAHoyVAVtNqBFnMhgFA-KgIM5EKhGYR2XMkolGR4IvwQkJGFBfLgCMQyELBMwIuwuGIEYgKKhDp0zKh6ANLPgqmBpmAAHIsAC2ECqjFOUCAA
Related Issues: #37241
The text was updated successfully, but these errors were encountered: