- Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
By DesignDeprecated - use "Working as Intended" or "Design Limitation" insteadDeprecated - use "Working as Intended" or "Design Limitation" instead
Description
Typescript is too lenient when accepting sub-classes in inherited types. For example:
interfaceBase{baseField: string;}interfaceChildextendsBase{childField: number;}vartest=function(callbackfn: (value: Base)=>void): void{callbackfn({baseField: ""});};varcallTest=function(){test(function(value: Child){console.log(value.childField);});};should not compile, as callbackfn requires a Base, not a Child. It is not correct that value is a child. If Child does not extend Base, then this causes a compile error as expected. However, if we add no explicit hierarchy between the classes, but change the example to
interfaceBase{baseField: string;}interfaceChild{baseField: string;childField: number;}vartest=function(callbackfn: (value: Base)=>void): void{callbackfn({baseField: ""});};varcallTest=function(){test(function(value: Child){console.log(value.childField);});};then the code still compiles. Both of these are errors that should hopefully be caught at compile time. It looks like typescript only checks to see if a callback parameter is valid if the classes have common members. This should be changed to a subset.
Metadata
Metadata
Assignees
Labels
By DesignDeprecated - use "Working as Intended" or "Design Limitation" insteadDeprecated - use "Working as Intended" or "Design Limitation" instead