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

[restrict-template-expressions] Invalid type "never" of template literal expression #3069

Closed
3 tasks done
carlpett opened this issue Feb 18, 2021 · 3 comments
Closed
3 tasks done
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended

Comments

@carlpett
Copy link

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

From the official TS docs on union exhaustiveness checks:

interface NetworkLoadingState {
  state: 'loading'
}

interface NetworkFailedState {
  state: 'failed'
  code: number
}

interface NetworkSuccessState {
  state: 'success'
  response: {
    title: string
    duration: number
    summary: string
  }
}

type NetworkState =
  | NetworkLoadingState
  | NetworkFailedState
  | NetworkSuccessState

function assertNever (x: never): never {
  throw new Error(`Unexpected object: ${x}`)
}

function logger (s: NetworkState): string {
  switch (s.state) {
    case 'loading':
      return 'loading request'
    case 'failed':
      return `failed with code ${s.code}`
    case 'success':
      return 'got response'
    default:
      return assertNever(s)
  }
}

Expected Result

A pass? Or something helpful about what to do instead?

Actual Result

repro.ts:25:41: Invalid type "never" of template literal expression. (@typescript-eslint/restrict-template-expressions)

Additional info
Running via ts-standard

Versions

package version
@typescript-eslint/eslint-plugin 4.14.1
@typescript-eslint/parser 4.14.1
TypeScript 4.1.3
ESLint 7.19.0
node v14.15.5
@carlpett carlpett added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Feb 18, 2021
@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended and removed triage Waiting for team members to take a look labels Feb 18, 2021
@bradzacher
Copy link
Member

This is intentional.
never is not known to be "string-like".
You'll get exactly the same result with unknown.

If you want to use this pattern, then use a disable comment.

@carlpett
Copy link
Author

In this case the never is on the NetworkState type, but I assume that doesn't really is the point?
And sorry if this is the wrong place to ask, is there a "better" pattern for exhaustive checks?

@bradzacher
Copy link
Member

bradzacher commented Feb 18, 2021

In this case the never is on the NetworkState type

What type a variable was before doesn't matter - all that matters is that in that branch TS thinks that it should be unreachable, and thus assigns it type never.

is there a "better" pattern for exhaustive checks

No. This is the correct pattern.

But if you want to throw errors with template strings whilst using restrict-template-expressions then you'll run into this problem. Just use a disable comment.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended
Projects
None yet
Development

No branches or pull requests

2 participants