question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Weaken type checks based on CFA with assertion conditions

See original GitHub issue

Too strict for tests.

cc @ahejlsberg

TypeScript Version: 3.7.x-dev.20191207

Search Terms:

Code

declare function assert(value: any): asserts value;
declare const set: Set<0>;

assert(set.size === 0);
set.add(0);
assert(set.size === 1);

Expected behavior: pass Actual behavior: This condition will always return ‘false’ since the types ‘0’ and ‘1’ have no overlap.(2367) Playground Link: http://www.typescriptlang.org/play/index.html?ssl=1&ssc=1&pln=6&pc=24#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXygGdCQYMAKANygmRAC4DUBPASkaJLMPmtpADcAKFCRYCMHkIZ4JDIwDKIDAB4ADAD5hQzqQpyAdISwAvBAF5L8Na2GGowYORvDdZcoeNn4l8-ACMtkA

Related Issues:

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
jcalzcommented, Dec 10, 2019

Let’s imagine that we disable overlap detection. Then what do you expect set.size to be at the end of your example code? I think it would end up looking like this:

declare function guard<T>(x: any, v: T): x is T;
declare const set: Set<0>;
if (!guard(set.size, 0 as const)) throw new Error(); // assert(set.size === 0)
if (!guard(set.size, 1 as const)) throw new Error(); // assert(set.size === 1)
set.size; // <--- never

Unless you want to narrow to never, I suspect you are using asserts functions in places you shouldn’t. Perhaps you want two assert() functions, one which performs CFA narrowing and one which doesn’t:

// keep using this one for your tests
function assert(x: any) {
    if (!x) throw new Error();
}

// use this one for things you actually want to narrow
function assertCFA(x: any): asserts x {
    assert(x);
}

Or, is there some explicit suggestion here for how to modify the behavior of TS assertion functions so that they behave well for your use case without disabling CFA narrowing entirely?

0reactions
falsandtrucommented, Dec 10, 2019

@ahejlsberg Can you make your new assertion functions usable for the existing tests? They don’t support some typical test patterns. Otherwise I have to revert the introduction of them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

cfa-candidate-cbt-checklist-2020.pdf
Scheduled exam appointment. Reviewed all testing policies. Confirmed valid international travel passport—the only type of identification accepted.
Read more >
Reasoning about parameterized components with dynamic ...
In this approach, when checks are excluded the underlying code is just as efficient as if assertions were “compiled out.” In addition, a...
Read more >
FOREIGN TRADE BARRIERS - USTR
The merchandise trade data contained in the NTE are based on total ... These are among the most difficult types of foreign practices...
Read more >
Final rule: Good Faith Determinations of Fair Value - SEC.gov
The rule will provide requirements for determining fair value in good ... types of valuation risk.39 As discussed below, we are reiterating ...
Read more >
vtable.md in doc/proposals – Cforall
type once as a parameter. Extensions may later loosen these requirements. 49. 50, Also note this applies to the final expanded list of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found