Skip to content

Assert functions do not constraint type when they are guarded by a truthy expression. #37295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mhevery opened this issue Mar 9, 2020 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@mhevery
Copy link

mhevery commented Mar 9, 2020

TypeScript Version: 3.8.3

Search Terms:: Assert Function

Code

function assertString(actual: any): asserts actual is string {}
function expectString(value: string) { }
let devMode: {} = {};

// This works as expected
const x: string|number = '' as any;
assertString(x);
expectString(x); // x is string!

// This fails
const y: string | number = '' as any;
// Expecting TS to correctly infer that `devMode` is always truthy and 
// hence the`assertString` sholud always execute therefore its 
// type assertion should hold.
devMode && assertString(y);
expectString(y); // y is string|number hence it fails

Expected behavior:

Assert functions should be assumed to always execute when they are guarded by an expression which is truthy.

Actual behavior:

Any sort of guard expression (even if know to be truthy) prevents the assert function from applying its constraint.

Playground Link:
Playground

Related Issues:

mhevery added a commit to mhevery/angular that referenced this issue Mar 10, 2020
…asserts

As of TypeScript 3.7, TypeScript supports [Assert Functions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions). This change adds assert types to our `assert*` functions.

We can't fully take advantage of this due to [Assert functions do not constraint type when they are guarded by a truthy expression.](microsoft/TypeScript#37295)
matsko pushed a commit to angular/angular that referenced this issue Mar 11, 2020
…asserts (#35964)

As of TypeScript 3.7, TypeScript supports [Assert Functions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions). This change adds assert types to our `assert*` functions.

We can't fully take advantage of this due to [Assert functions do not constraint type when they are guarded by a truthy expression.](microsoft/TypeScript#37295)

PR Close #35964
matsko pushed a commit to angular/angular that referenced this issue Mar 11, 2020
…asserts (#35964)

As of TypeScript 3.7, TypeScript supports [Assert Functions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions). This change adds assert types to our `assert*` functions.

We can't fully take advantage of this due to [Assert functions do not constraint type when they are guarded by a truthy expression.](microsoft/TypeScript#37295)

PR Close #35964
@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 11, 2020
@RyanCavanaugh
Copy link
Member

We don't trust the results of our own truthiness analysis for these situations because there are legitimate situations where a falsy value might inhabit a truthy position. We also believe that code like x && y is written because x is possibly falsy (otherwise why would you do it...?). It seems like you intend to strip this out later, so I'd recommend just using some other approach.

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants