Skip to content

Type narrowing should be able to make generics irrelevantΒ #54601

@ShacharHarshuv

Description

@ShacharHarshuv

Bug Report

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Ref<T> = {
  value: T | null;
}

function convertRef<T, U>(ref: Ref<T>, callback: (t: T) => U): Ref<U> {
  if (ref.value === null) {
    return ref; // error: Type 'Ref<T>' is not assignable to type 'Ref<U>'
  }

  return {
    value: callback(ref.value),
  }
}

πŸ™ Actual behavior

There is an error on: Type 'Ref' is not assignable to type 'Ref'

πŸ™‚ Expected behavior

Because of the if condition, ref value should be narrowed to { value: null }, which should be assignable to Ref<U>, so there shouldn't be any error.

(The ticket name might not be the best, feel free to rename if needed)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions