Let's say I have 2 types with the same property name but different data types:
type A = {
myProperty:string
}
type B = {
myProperty:number
}
If I extend both using an interface, I see an error as expected:
interface C extends A, B {}
Interface 'C' cannot simultaneously extend types 'A' and 'B'.
Named property 'myProperty' of types 'A' and 'B' are not identical.
But if I use a type to extend both, there is no exception. In this case, what's the resulting data type of myProperty
? Is there a way to check the resulting data type?
type C = A & B; // no error
I can't set a value for myProperty
(string or number) if C is declared as a type. What's the best way to handle this scenario?
function F(myParameter:C) {
console.log(myParameter);
}
F({myProperty: 90});
(property) myProperty: never
Type 'number' is not assignable to type 'never'.ts(2322)
index.ts(3, 3): The expected type comes from property 'myProperty' which is declared here on type 'C'
Same error if I call the function as F({myProperty: 'test data'});
.
Playground: https://codesandbox.io/s/typescript-playground-export-forked-dglbq5?file=/index.ts