Can you declare a object literal type that allows unknown properties in typescript?

前端 未结 4 1549
[愿得一人]
[愿得一人] 2020-12-20 12:16

Essentially I want to ensure that an object argument contains all of the required properties, but can contain any other properties it wants. For example:

fu         


        
4条回答
  •  盖世英雄少女心
    2020-12-20 12:52

    Yes, you can. Try this:

    interface IBaz {
        baz: number;
        [key: string]: any;
    }
    
    function foo(bar: IBaz) : number {
        return bar.baz;
    }
    
    foo({ baz: 1, other: 2 });
    

提交回复
热议问题