8000 Narrowed types do not flow into callback functions · Issue #44869 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content
Narrowed types do not flow into callback functions #44869
@kolpav

Description

@kolpav

Bug Report

Previous discussion #37255 (comment)

Title is borrowed from @danielrentz #37255 (comment) I don't know if its accurate description of the problem.

🔎 Search Terms

None

🕗 Version & Regression Information

Any

⏯ Playground Link

Playground

💻 Code

import { FunctionComponent, useMemo } from 'react'

export interface ButtonProps {
  size?: 'sm' | 'base' | 'lg'
}

export const Button: FunctionComponent<ButtonProps> = props => {
  props.size ??= 'base'
  const sizeClassNames = useMemo(() => {
    return {
      sm: 'px-3 py-2 text-sm',
      base: 'px-4 py-2 text-sm',
      lg: 'px-4 py-2 text-base'
    }[props.size] // << Type 'undefined' cannot be used as an index type.(2538)
  }, [props.size])

  return (
    <button
      type="button"
      className={sizeClassNames}
    >
      Button text
    </button>
  )
}

🙁 Actual behavior

Type 'undefined' cannot be used as an index type.(2538)

🙂 Expected behavior

props.size should not be considered as undefined

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

      0