prescottprue / react-redux-firebase

Redux bindings for Firebase. Includes React Hooks and Higher Order Components.
https://react-redux-firebase.com
MIT License
2.55k stars 559 forks source link

Typescript - `isLoaded` should narrow type #955

Closed lukeKonopka closed 4 years ago

lukeKonopka commented 4 years ago

Do you want to request a feature or report a bug? feature

What is the current behavior? Using isLoaded helper with argument of type T | undefined doesn't narrow the type of the argument to T.

function renderTodos(todos: string[]) {
  // ...
}
function Todos({ todos }: { todos: string[] | undefined }) {
  // const todos: string[] | undefined
  if (!isLoaded(todos)) {
    return <span>Loading...</span>
  }

  // type error here - todos is still string[] | undefined
  return <todos>{renderTodos(todos)}</todos> 
}

What is the expected behavior? isLoaded function should return a type predicate so that invoking it with argument of type T | undefined narrows it down to T

function renderTodos(todos: string[]) {
  // ...
}
function Todos({ todos }: { todos: string[] | undefined }) {
  // const todos: string[] | undefined
  if (!isLoaded(todos)) {
    return <span>Loading...</span>
  }

  // const todos: string[]
  return <todos>{renderTodos(todos)}</todos> 
}
prescottprue commented 4 years ago

Thanks for the PR and for closing!