Skip to content

Generic loses generic behaviour under certain conditions.Β #56377

Closed
@coolCucumber-cat

Description

@coolCucumber-cat

πŸ”Ž Search Terms

generics, generic loses genericness, generics with object types

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.0-beta#code/C4TwDgpgBAslC8UDeAoAkAQwFzKgEw2GygHIMSoBfdAIxyX0OJJourQGN7GicSO2KaigBmAVwB2HYAEsA9hKgSAPAHkoEAB7AIEvAGcoAawgg5I2AD4AFHJyqAlDhgBtVQF0XJAkRLvk6ABOEMBigYpkJEIoKBLWLCQOKEA

πŸ’» Code

type M = {
	a: { data: 'a' }
	b: { data: 'b' }
	c: { data: 'c' }
}

function n<O extends keyof M>(o: O): M[O]['data'] {
	return 'a' // expected error
}

n('b')

πŸ™ Actual behavior

It allows 'a', 'b' or 'c' instead of the generic type which is supposed to be the same as the input. The same problem occurs with assigning variables or functions inside the other function. Although the type must be M[O]['data'] and it says that it is that, it acts as if it were no longer generic, as in M[keyof M]['data'].

πŸ™‚ Expected behavior

I woud expect it to be M[O]['data'].

Additional information about the issue

You don't have this issue if you remove the object with the data key and replace it with the value of data, for example ''a' instead of { data: 'a'}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions