Skip to content

Property does not exist on selected type, but available via callback #60835

Closed
@edyth933

Description

@edyth933

🔎 Search Terms

I was thinking this code works like how the addEventLister or on behavior happens. Are there other less complex solutions?

🕗 Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAKgFhACgJwPZgM5QLxQN4BQUxUAZgJbIbABc+RJjAhsgOYAMd1y5AdqwG4GjYi1YBGLsB78hjAL7CoGCAGNUvACZ1CIkmM5ReAVwC2AIwjI5eqGIBMdc6lQAbCE143iixsDiU2vS2dmwAzFIygkrMbAAsdCYWVt5QiooEmmquLNDqvNRQ0iAAwuZ0ADwA0lAQAB7AEFpYANYQIKiksAgo6BgAfAAUYGhgAHJMphB0VQA0UKrlUMOjGHTwSKsA2lUAugCUOP1QAG6o5JqH2MdnFwQExWWDAOQq+ZrP8yN9RwsaGG4IAA6VyoVgrPpAhz7fb3fKFYoAMWMvFUOCg1VqDSamla7U63U2fSG33Gk2mUDmUFJa0JvUwOwOv10JHIXQhZKmOFwrzUGg+hxZenhgJBYI5GChbHssIUBEUQA

💻 Code

type TheProps = {
    first: {
        arg0: string;
        arg1: string;
    }
    second: {
        arg0: number;
        arg2: boolean;
    }
    third: {
        arg3: string;
        arg4: number;
    }
}

declare const tryCb: <K extends keyof TheProps>(propName: K, cb: (props: TheProps[K]) => void) => void

tryCb('second', props => console.log(props.arg2)) // => no error (although this should also be an error)

const tryFunc = <K extends keyof TheProps>(propName: K, props: TheProps[K]) => {
    if (propName == 'second') {
        console.log(props.arg2) // => error here
    }
}

🙁 Actual behavior

Property does not exist on selected type, but available via callback

🙂 Expected behavior

no error occurs same as using callback

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions