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.

non-overlapping cast error should suggest casting to the narrowest possible type instead of `unknown`

See original GitHub issue

Suggestion

🔍 Search Terms

overlap cast error unknown conversion

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

⭐ Suggestion

given the following three types: A, B extends A and C extends A, this error message could be improved if instead it suggested the narrowest common ancestor type instead of unknown before:

Conversion of type ‘B’ to type ‘C’ may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to ‘unknown’ first.

after:

Conversion of type ‘B’ to type ‘C’ may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to ‘A’ first.

📃 Motivating Example

interface A {
    a: number
}

interface B extends A {
    b: number
}

interface C extends A {
    c: number
}

interface D {
    d: number
}

declare const foo: B

// Conversion of type 'B' to type 'C' may be a mistake because neither type sufficiently overlaps with the other.
//  If this was intentional, convert the expression to 'unknown' first.
foo as C

//when using unknown, it's easier to accidentally cast to a completely unrelated type:
foo as unknown as C
foo as unknown as D //no error

//when using A, you get an error if you do that:
foo as A as C
foo as A as D //error

💻 Use Cases

  • makes casting a little bit safer
  • helps users learn the casting rules more easily
    • when i was new to typescript i had no idea why casting to unknown fixed these overlapping type errors, and i think many users probably feel the same and just blindly follow the suggestion in the error message without having a clear understanding of what it means

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Josh-Cenacommented, Jan 10, 2022

makes casting a little bit safer

Not looking onto the feasibility side, if the cast is between non-overlapping ones, isn’t it already straight unsafe?

0reactions
mrienstracommented, Oct 23, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I cast custom type to primitive type? - Stack Overflow
You cannot cast from a custom to a primitive without erasing the type first. unknown erases the type checking. Try : myRating as...
Read more >
Conversion of type 'X' to type 'Y' may be a mistake in TS
The error "Conversion of type 'X' to type 'Y' may be a mistake because neither type sufficiently overlaps with the other" occurs when...
Read more >
The unknown Type in TypeScript - Marius Schulz
TypeScript 3.0 introduced a new unknown type which is the type-safe ... We can narrow the unknown type to a more specific type...
Read more >
How are the links under "current work items" on the Change ...
(route workflow) --> Then workflow with action moves change to a status = auth andassigns an approver type via automation script via action...
Read more >
Unicode Objects and Codecs — Python 3.11.1 documentation
The “legacy” Unicode object will be removed in Python 3.12 with deprecated APIs. ... cast to UCS1, UCS2 or UCS4 integer types for...
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