-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
FixedA PR has been merged for this issueA PR has been merged for this issue
Description
There is a difference in how TypeScript infers the type automatically based on type of the argument we pass in:
- how the value is declared (inline or by reference)
- whether the value is an inline object with non-arrow function
The error is reproduced if generic parameter has default value undefined (this is mandatory in my case.)
🔎 Search Terms
- "generics in constructor"
- "type argument inference undefined"
🕗 Version & Regression Information
- This is the behavior in every version I tried from 3.9.7 to 5.9.3
⏯ Playground Link
💻 Code
This issue with function:
function make<T, S=T, D=undefined>(value: T, options: NewOptions<T, S, D>) {
return {
v: value,
data: options?.data,
sourceValue: options?.sourceValue ?? value,
finaleValue: options?.finaleValue,
};
}
export type NewOptions<T, S, D> = {
finaleValue?: T,
sourceValue?: S,
data: D,
};
const instance_withError = make(0, {
// Error here: `Type '{ test(): number; }' is not assignable to type 'undefined'.(2322)`?
data: {
test() {
return 123;
},
},
});
const data = {
test() {
return 123;
},
};
// no error here
const instance1 = make(0, {
// data is not inline object, BUT object with non-arrow function
data,
sourceValue: 'test',
finaleValue: 1,
});
// no error here
const instance2 = make(0, {
// data is inline object, BUT object with arrow function
data: {
test: () => {
return 123;
},
},
sourceValue: 1,
finaleValue: 2,
});
const sV1 = instance2.sourceValue;
// ^?
const v1 = instance2.v;
// ^?
const sV2 = instance1.sourceValue;
// ^?
const v2 = instance1.v;
// ^?
// Should be `{ test(): number; }`
const data_invalid = instance_withError.data;
// ^?
// Should be `{ test(): number; }`
const data1 = instance1.data;
// ^?
// Should be `{ test: () => number; }`
const data2 = instance2.data;
// ^?🙁 Actual behavior
- An error occurs:
Type '{ test(): number; }' is not assignable to type 'undefined'.(2322) datatype isundefined
🙂 Expected behavior
- No error occurs
datatype is{ test(): number; }
Metadata
Metadata
Assignees
Labels
FixedA PR has been merged for this issueA PR has been merged for this issue