Repository: TypeScript
Issue: `keyof T['prop']` becomes never when the object contains a context-sensitive function
Labels: Bug,Help Wanted

# Bug Report

<!--
  Please fill in each section completely. Thank you!
-->

### 🔎 Search Terms

Not sure how to put this but:

- recursive type
- function
- parameter
- generics

### 🕗 Version & Regression Information

Tested on these versions:

- typescript@5.1.3
- typescript@5.2.0-dev.20230716 dev

### ⏯ Playground Link

[Playground Link](https://www.typescriptlang.org/play?ts=5.2.0-beta#code/C4TwDgpgBAKuEB4BQVYEEDGGJmBAJgNIQgDOUEAHngHb7mnABOAljQOYA0KsckF1CHXJ9EMTNlwFiZAHwBeAIY0QSWVHlQA3jwD0AKn08o+qAHUIAcibQArqTbsowABbQA1iXIB7AGbOXFnIwJm9IJlBOAMVgKCCoAHcXVVQTRLtSaEUoGwxbJgcAN2hQSAA6Y31dHm8AIwArAH4ALigAJQgMbyZ8BHEsHDwiLyiARlk9Q0qoACl7WN9ugOhSRU8oPziAGy35phiWbxoyqGmASRplqAwWJjyAW0ZlbB9-V2ha2ydXeISWHagtWgNAgxSYAG0ALoVVJVHieMitAAUCM2MFE4MsdXqlkhAEooZMjLCoGgoL5bDQMMBDpc-q4oNkwIp9vcIHgmHEaA58CU3DlOvkiiV4DDUKYYIFyMwWOx2BACldPuwxSZqqgZXKFQAxGgtKBI5ms1o0Wz3IFMPEadSFbwsfBE6aS37-LaMrYJRRkQHQdHwKI2dgs-BbCCkV6JNzvJjTJksxRsjlxci8jBbFkELlQAAGmvlTF12Y2nJo3mAqrhqFILm8ti2+AAQhA-ZB9S2II6Sc7yH8AUCc6j-O2i4pyFRINTMyx-HHWeyFdN4qXYqn0zZ8Fm8zqaFFPrEWLFe27gPlLsBvDnKbzfGwCNnYwxvEdGeRZwn55z4quMxu2NEFpS1K0hW5xvPyQJbN4CRQCEYQKqAyYwaOmQbueUDXreojTO8zjwIkro+i+AgTkMzTYawWoFjQyJWvI6haAAvpU0wABJQaCCoBC6AJbpyOHKmRJK8bqyJGgmtH0UxsLqlA1a1vWTYyH47b6oOvDwEgTFIF03IrhAN4gqIGhQH0EiDNIXgCLQ9CyTKHCyEiEk8AgOmMOp-BUNZIjwKZAxSMMZBRO2sgOaUECtO2Em4ZASBIBhhk+ZYihgGAoaWFAAA+UCWLUyh5ZYDl4kiOioNirQlakyWpeFUCjNwqSAnlNCKK0ow8Ax9VQAipCtOCxjZVVaU8JC3CduKsDeHKobLJk1zePcbI0MA0oXpk0A1tB-FfIk3TuKQFYycJ1EGlFjFRA1UC6LoO00JYsSwfgtjYOhLCKOwpaMCwGCkHo11HaJ8b3KdHWXX9eGWIwrAcOlS5li+DjvYotTTWhYXZSCYKWLFqAGMS40wJN7Ao24s1dAtQjLc4q0QNA+Cve93ifd9XKLEw9wHEcB2-bJNZ1o2zaiCadZbJ1V08-J-NKb47ZCzsml4kgQA)

### Code

```ts
type Type<
  TAcceptedKeys extends string,
  TType extends Type<TAcceptedKeys>=any
> = {
  /**
   * We're using the keys of this property, that is why
   * we use a recursive type.
   */
  obj?: Record<TAcceptedKeys, 1>
  /**
   * Just for the sake of illustration. 
   * In the circumstances of the bug this will be never[].
   */
  keys: (keyof TType['obj'])[]
  /**
   * A function with a parameter inside the recursive type.
   * This triggers the bug.
   */
  triggerFn?: (param: number) => void
  /**
   * This will always be TType, regardless of whether
   * a parameter is declared in `triggerFn` or not.
   */
  shouldBeTType?: TType
  /**
   * This will be `keyof TType` as expected if a parameter
   * is not declared in triggerFn, but it will turn to `undefined`
   * as soon as a parameter is declared in that function.
   *
   * If the below property is passed to defineType
   * the type will be as expected:
   * triggerFn: () => {}
   *
   * However this will trigger the bug:
   * triggerFn: (param) => {}
   */
  shouldBeKeyofTType?: keyof TType
}

const defineType = <TAcceptedKeys extends string>() =>
  <const TType extends Type<TAcceptedKeys, TType>>(type: TType) => type

defineType<'apple' | 'banana'>()({
  obj: {
    apple: 1,
    banana: 1
  },
  keys: [
    'apple'
  ],

  /**
   * Toggle these comments to see how the bug works.
   */
  triggerFn: () => {},      // won't produce diagnostics
  // triggerFn: (param) => {}, // type 'string' is not assignable to type 'never'

  /**
   * Toggle these comments to see diagnostics information.
   */
  // shouldBeTType: null,
  // shouldBeKeyofTType: null
})
```

### 🙁 Actual behavior

The presence or absence of a parameter in an object member function has effect on this object type in certain circumstances.

### 🙂 Expected behavior

It shouldn't.