Change default inference from arrays to tuples and primitives to literals
See original GitHub issueI 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:
- Created 3 years ago
- Reactions:4
- Comments:18 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.See #32758