Hegel: Type "[T]" is incompatible with type "Array<T>"

Created on 3 May 2020  路  2Comments  路  Source: JSMonk/hegel

function ensureArray<T>(value: T | Array<T> | null | undefined): Array<T> {
  if (value == null) {
    return []
  }

  if (typeof value === 'object' && value instanceof Array) {
    return value
  } else {
    return [value]// Type "[T]" is incompatible with type "Array<T>"
  }
}

try

bug

All 2 comments

surprisingly (for me) this works

function ensureArray<T>(value: T): Array<T> {  
  return [value]
}

try

Hmmm, it's works too

function ensureArray<T>(value: T | Array<T> | null | undefined): Array<T> {
  if (value == null) {
    return []
  }

  if (value instanceof Array) {
    return value
  } else {
    value
    return [value]
  }
}

try

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Raynos picture Raynos  路  4Comments

devhammed picture devhammed  路  3Comments

karimsa picture karimsa  路  4Comments

trusktr picture trusktr  路  5Comments

antonkatz picture antonkatz  路  3Comments