Skip to content

Generic inference issue with callbacks #48009

Closed
@ipb26

Description

@ipb26

Bug Report

🔎 Search Terms

Generic object inference, Generic object inference based on properties, Generic object inference callback.

🕗 Version & Regression Information

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

⏯ Playground Link

https://www.typescriptlang.org/play?#code/FAFwngDgpgBAwgewLYCMEB4DqCBOBrAZwBoYARBKAgOxG3wD4YBeGAb2AEgRKQBGALhgAKAJTNGdQp24EQAJkFCAbgEMANgFcog2TgCWVAOZimjcpRqTOAMw1UAxgOGrN2mJIInGug4Zt37BWd1LUFzalpcPC8YHyNgAF9gezUVAgJ4ZDQ4VPT0ABUSADVGdg57BCpdDXsQXCEcKBUAE0q1MBhmqGsDPRA9SsFEVAxCmBKxMqSkgHoZ8KoAchAYAHco5MrZGAqR3mYYKihVzJGctIIhMpk+RRjeImkeIOUQqBi5R45bBycXLXEMH+UAAdHUADIIezqKAAZRA+iMoi+P0CgmBgOBYIQkOhajhCN8yLmAFEAEpkgDyZJgAFogW8YHoMghrDBwNAYHY8FQEKsqIkRMA5h5NlUVrs0HIDkcTsNsrlLtceE5RICHk9ZC9gTpCUYPiQ5gBJGAACxUSlgdVi0HsemsHR1sT1hjpMCglqo7NNCA0hlNTJAiwyvJWhigR309jdfWDMHUqxUYAycVdKiozW9sF2ED0+JwMB5fIyfTWfVNBm9zJgqP6lXdnqZbJNrSWKwItvtHT6IP8v3RjNMDNc2NxMPhiMMyL7aOHAKHWIhUPHLunCSFQA

💻 Code

type Combo<Works, DoesntWork> = {
	test1: () => Works
	test2: (value: string) => DoesntWork
	func1: (value: Works) => string
	func2: (value: DoesntWork) => string
}
class ComboClass<T, V> {
	constructor(readonly definition: Combo<T, V>) {
	}
}
//Doesn't work
const combo1 = new ComboClass({
	test1: () => 1,
	test2: (value) => 2,
	func1: value => value.toLocaleString(),
	func2: value => value.toLocaleString(),//ERROR - value is of type unknown
})
//Works
const combo2 = new ComboClass({
	test1: () => 1,
	test2: (value: string) => 2, //I have to specify value: string - even though it's not generic - it's always string. Within this function, the compiler knows it's a string, but it breaks the inference for some reason.
	func1: value => value.toLocaleString(),
	func2: value => value.toLocaleString(),
})

🙁 Actual behavior

🙂 Expected behavior

See code comments.

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