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

Request to expose getBigIntLiteralType on the TS Type Checker #58563

Open
6 tasks done
kirkwaiblinger opened this issue May 17, 2024 · 0 comments
Open
6 tasks done

Request to expose getBigIntLiteralType on the TS Type Checker #58563

kirkwaiblinger opened this issue May 17, 2024 · 0 comments

Comments

@kirkwaiblinger
Copy link

kirkwaiblinger commented May 17, 2024

πŸ” Search Terms

bigint, checker api, checker, external api

βœ… Viability Checklist

⭐ Suggestion

cc @jakebailey

Add getBigIntLiteralType() to the checker API.

πŸ“ƒ Motivating Example

I'm working on some possible improvements to typescript-eslint's strict-boolean-expressions and no-unnecessary-conditions rules. These need to check for assignability of possibly falsy types.

So far, we can check

    const falsyTypes = [
      {
        value: null,
        type: checker.getNullType(),
      },
      {
        value: undefined,
        type: checker.getUndefinedType(),
      },
      {
        value: false,
        type: checker.getFalseType(),
      },
      {
        value: '',
        type: checker.getStringLiteralType(''),
      },
      {
        value: 0,
        type: checker.getNumberLiteralType(0),
      },
      {
        value: -0,
        type: checker.getNumberLiteralType(-0),
      },
      {
        value: NaN,
        type: checker.getNumberLiteralType(NaN),
      },
    ]
    
    // roughly,
    if (falsyTypes.some(falsyType => checker.isTypeAssignableTo(falsyType.type, type)) {
        // condition is necessary, don't report an error.
    }

, but we're missing the ability to check assignability of 0n.

Looks like the other falsy literal types were exposed in #50694, #52473, for similar reasons, but getBigIntLiteralType was not included.

πŸ’» Use Cases

  1. What do you want to use this for?
    • This would be beneficial for a typescript-eslint rule
  2. What shortcomings exist with current approaches?
  3. What workarounds are you using in the meantime?
    • (answer to 2 and 3) The type flag PossiblyFalsy exists, and currently powers the no-unnecessary-condition rule. However, it has some drawbacks, such as not being set on the {} type, or other "object" types to which falsy values are assignable, see, e.g.let x: {toString: () => string} = 0n, which can cause false positives in the rule. Furthermore, the PossiblyFalsy flag doesn't indicate why the type is possibly falsy, and it could have significant value to be able to tell a user which falsy types are assignable to a type.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant