validation - Typescript: Declare function that takes parameter of exact interface type, and not one of its derived types -
interface { a: number; } interface ii extends { b: number; } function f(arg: i) : void { // arg without trimming properties (logical error) console.log(arg); } const obj: ii = { a:4, b:3 }; f(obj);
what want make function f
accept objects of type i
, not type ii
or other derived interface
you cannot achieve in typescript, in general, in languages cannot make such constraint. 1 principle of object oriented programming can pass derived class base class expected. can perform runtime check , if find members don't expect, can throw error. compiler not achieve this.
Comments
Post a Comment