I'm encountering an issue with TypeScript that I'm not sure if it's a bug or an unsupported feature. Here is a Minimal Viable Example (MVE) of the problem:
interface ColumnOptions<R> {
valueFormatter(params: R): string;
valueGetter(params: string): R;
}
const getColumn = function<R>(options: ColumnOptions<R>) {};
getColumn({
valueFormatter: params => 'hello',
valueGetter: params => ({
nr: 1,
str: 'hello',
}),
});
The scenario involves defining an Ag-grid column and aiming to have the parameter of valueFormatter
inherit from the return type of valueGetter
. In the above case, the parameter type of valueFormatter
(R) is automatically inferred as unknown
. However, explicitly typing or removing the parameter of
valueGetter</code, like this:</p>
<pre><code>getColumn({
valueFormatter: params => 'hello',
valueGetter: () => ({
nr: 1,
str: 'hello',
}),
});
results in R being inferred correctly. This discrepancy made me consider that there might be a bug in TypeScript, but it could also be an unsupported behavior. I didn't want to raise an issue on GitHub without confirming. The tests were carried out using TS versions 3.8, 3.7, 3.5, and 3.0.