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.

Change default inference from arrays to tuples and primitives to literals

See original GitHub issue

I am working on functional project now and I’m getting a lot of errors related to too wide type inference for values. It looks like

function test(a: { data: 1 | 2 }) { return a }
const a = { data: 2 }
// Types of property 'data' are incompatible.
// Type 'number' is not assignable to type '2'.ts(2345)
const b = test(a)

There’re really many errors like this, and the main problem is that you can convert type A to type B, but not vice versa.

type A = { data: 2 }
type B = { data: number }

So, I ask you to add a new compiler option (to not break existing code) that will make type checker infer types as narrow as possible. It will be very helpful for functional programming.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:18 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
awerloguscommented, Jun 29, 2020

@jgbpercy the expression as const is being used to solve two different problems in the same time: the first is inferring types as literal as possible (that thing which I’m asking for in this issue), and the second is marking arrays or object properties as readonly (this is not my proposal). We need a new type checker mode to solve the first problem only. Other than that is completely as you said.

2reactions
RyanCavanaughcommented, Jun 11, 2020

See #32758

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Everyday Types - TypeScript
In this chapter, we'll cover some of the most common types of values you'll find in JavaScript code, and explain the corresponding ways...
Read more >
Mutable and Immutable Arrays - Learn TypeScript - Educative.io
In this lesson, you will learn about two different syntaxes to define an array. We'll cover the following. Mutable arrays; Immutable ...
Read more >
Zod | Documentation
z.enum is a Zod-native way to declare a schema with a fixed set of allowable string values. Pass the array of values directly...
Read more >
Types — The Swift Programming Language (Swift 5.7)
There are two compound types: function types and tuple types. ... defined in the Swift language itself and describes the type inference behavior...
Read more >
Tuple - The Crystal Programming Language
A Tuple is typically created with a tuple literal: ... An array of tuples of Int32, String and Char Array({Int32, String, Char}) ...
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