Skip to content

Strange array + push inference behavior with spread operator #39470

Closed
@CapsAdmin

Description

@CapsAdmin

TypeScript Version: 3.7.x-dev.201xxxxx

array with push inference failing

Code

const Foo = (arg: { val: number }[]) => {
	return { val: 1 }
}

let arr = []

arr.push({
	bar: 1, // ERROR: argument of type X is not assignable to parameter of type Y, etc. 
	...Foo([]),
})

Foo(arr) // comment out this and uncomment below

// this circumvents the error
// let lol = arr; Foo(lol)

Expected behavior:
Typescript should be able to guess the array type when spreading out {val: 1} to be {bar: number, val: number}[].

Actual behavior:
It errors with a confusing error. It's circumventable by doing let lol = arr; Foo(lol)

Playground Link:
Playground Link

Related Issues:
Not really sure what to search for.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions