In my code example, I have two interfaces that both extend a common base interface. The "String" function takes an argument of type "StringAsset". My expectation was that if I were to call the "String" function and pass it a value of "NumberAsset", TypeScript would show a typing error. However, this is not what happened.
It seems that the function is implicitly accepting "BaseAsset" arguments even though it's explicitly typed as "StringAsset".
How can I ensure that subtypes only accept their specific subtype and not a sibling type?
interface BaseAssets {
message?: string
}
interface StringAssets extends BaseAssets {
length?: number
maxLength?: number
minLength?: number
}
interface NumberAssets extends BaseAssets {
value?: number
maxValue?: number
minValue?: number
}
function String(assets: StringAssets){
//Some code to run
}
const asset: NumberAssets = {
minValue: 10,
maxValue: 100,
}
String(asset) //Expected typing error