Skip to content

Sorting an array removes type inference #34099

Description

TypeScript Version: 3.7.0-dev.20191011

Search Terms: type inference

Code

function getAllValuesUnsorted(m: ReadonlyArray<ReadonlyArray<string>>): ReadonlyArray<string> {
	// OK
	return Array.from(flatten(m.values()))
}

function getAllValuesSorted(m: ReadonlyArray<ReadonlyArray<string>>): ReadonlyArray<string> {
	// Type 'unknown[]' is not assignable to type 'readonly string[]'.
	return Array.from(flatten(m.values())).sort()
}

function* flatten<T>(a: Iterable<Iterable<T>>): Iterable<T> {
	for (const xs of a)
		for (const x of xs)
			yield x
}

Expected behavior:

No error.

Actual behavior:

a.ts:6:2 - error TS2322: Type 'unknown[]' is not assignable to type 'readonly string[]'.
  Type 'unknown' is not assignable to type 'string'.

Activity

  1. RyanCavanaugh commented on Oct 30, 2019

    @RyanCavanaugh
    Member

    Presumably generic inference isn't lining up the this through the return type of sort to produce an inference candidate for flatten

  2. added
    Domain: check: Type Inference
    on Oct 16, 2025
  3. added
    Domain: check: Order Dependence
    on Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDomain: check: Order DependenceDomain: check: Type Inference

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions