Skip to content
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

No narrowing on assert.strictEqual() #48244

Open
mrtnlrsn opened this issue Mar 14, 2022 · 3 comments
Open

No narrowing on assert.strictEqual() #48244

mrtnlrsn opened this issue Mar 14, 2022 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@mrtnlrsn
Copy link

mrtnlrsn commented Mar 14, 2022

Bug Report

assert.strictEqual(a, b) and assert.equal(a, b) does not result in typescript narrowing like assert(a === b)

🔎 Search Terms

assert, assert.equal, equal, narrowing

🕗 Version & Regression Information

Always (I assume)

Original feature implemented in #8010

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about equal

⏯ Playground Link

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgZxlYBjeBDZdfICmscAvnAGZQQhwDkBxMdAUCzAJ5iFwCCcAXkQs4ouJ24AuetlZi4AO2kKAriABGxFqTYSeAIUHD5e6XXVyxyaanQKA5traEAHpBIUVCrMAgLKAIwAFC5w0vwAPnD6AJTGYoywIQB0eoICQgx0MSJiGH7IEAA2hMlFEPYpCnFwAPS1cADyANJOLK7u8J7eML7+FABMIWF8cFGx8aKJMMm2mDAAogCOKthFKXoANDLZuaL5CoUlZRVVcfVwAGK8AJIAMtIACtTcsBz0CnRwACYQhHgKCDwVzAVBwPziLg8OiRaJ0ZJwZ4QV6cD5fX7-RRAuAgsEQtJ0fTwpxAA

💻 Code

import { strict as assert } from 'assert'

type A = {
    type: 'a'
    n: number
}

type B = {
    type: 'b'
    s: string
}

export function f1(x : A | B) {
    assert(x.type === 'a')
    console.log(x.n)  // OK
}

export function f2(x : A | B) {
    assert.strictEqual(x.type, 'a')
    console.log(x.n) // FAIL: Property 'n' does not exist on type 'A | B'. Property 'n' does not exist on type 'B'.
}

🙁 Actual behavior

No typescript narrowing in function f2()

🙂 Expected behavior

Typescript narrowing in function f2() since it is equavalent to f1()

@MartinJohns
Copy link
Contributor

This is no bug in TypeScript. The type annotation of strictEqual does not narrow x, because it's unaware of x. I'm not aware of any way to type this in TypeScript.

The assert example works because TypeScript sees the whole expression x.type === 'a' for narrowing purposes, but this is not the case for strictEqual. Here the asserts function is completely unaware of x, it only sees x.type.

@mrtnlrsn
Copy link
Author

@MartinJohns Thanks for your reply.

OK, then lets call it a feature request.

This is definitely the logical behaviour that would be expected by most users, even though there may be technical reasons it does not work like that.

@fatcerberus
Copy link

Yeah, the only way to type an assertion function is either asserts x (asserts a whole expression) or asserts x is T (a type guard). Implementing this suggestion would require the ability to say asserts x === y, which is currently not possible.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Mar 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants