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.

Infer typeof self if unspecified on cast

See original GitHub issue

Suggestion

🔍 Search Terms

  • type casting
  • implicit typing
  • syntactic sugar

✅ 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

It would be a lot cleaner if when you cast a type to some other type (for example NonNullable) you don’t have to specify typeof self.

📃 Motivating Example

if you have an Array that could be undefined you can filter out undefined like so

const array = ["one", undefined, "two"].filter(Boolean);
console.log(array) // ["one", "two"]

but in this case the type would still be (string | undefined)[] so it would cause linting errors later in the code. While this may be specific to the .filter method, it may be unreasonable to expect all methods accurately account for type checks like this. So one easy way to fix this would just be to do

const arrayUndefined = ["one", undefined, "two"]

const array = arrayUndefined.filter(Boolean) as NonNullable<UnArrayify<typeof arrayUndefined>>

what I’m suggesting is that instead of requiring you do typeof arrayUndefined you can instead do an implicit cast to make the code cleaner

const arrayUndefined = ["one", undefined, "two"]

// it's implied that UnArrayify would be called with <typeof array.filter(Boolean)>
const array = arrayUndefined.filter(Boolean) as NonNullable<UnArrayify>

💻 Use Cases

I would like this because it would result in cleaner typing for unhandled edgecases in the typescript linter

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
MartinJohnscommented, Aug 30, 2021

You can achieve something like that trivially using a helper function without weird and confusing type shenanigans:

function asMutable<T>(value: T): Mutable<T> { return value; }

const context = asMutable(await import("github")).context);
3reactions
MartinJohnscommented, Aug 29, 2021

What do you mean by type guard? Do you mind providing an example

function isNotUndefined<T>(value: T | undefined): value is T {
  return value !== undefined;
}

const array1 = ["one", undefined, "two"].filter(Boolean); // type is (string | undefined)[]
const array2 = ["one", undefined, "two"].filter(isNotUndefined); // type is string[]

No type-assertion needed. Every type-assertion is a potential issue, because you overwrite the type-system and tell the compiler “shh… trust me, it’s this type” (even if this may not be the case).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ensure that an inferred type is assignable to a specified one ...
I have sometimes the case, that I want to ensure that a specific value V (like an object) is assignable to a given...
Read more >
Unable to infer type from union with undefined when using ...
I'm getting some wierd behavior when trying to match possibly undefined type of a value from a mapped type with a generic union...
Read more >
List of all issue types - Infer Static Analyzer
Here is an overview of the issue types currently reported by Infer. ... Reported as "Checkers Immutable Cast" by immutable-cast.
Read more >
Five tips I wish I knew when I started with Typescript - codeburst
In the above code snippet, typescript inferred the type of our rest object in ... mark will cast to your type T minus...
Read more >
typing — Support for type hints — Python 3.11.1 documentation
It is possible to declare the return type of a callable without specifying ... objects kept in containers cannot be statically inferred in...
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