{"id":4543,"date":"2016-06-19T02:22:31","date_gmt":"2016-06-19T02:22:31","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4543"},"modified":"2016-06-19T02:22:31","modified_gmt":"2016-06-19T02:22:31","slug":"typescript-type-definition-function-variable-number-parameters","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/typescript-type-definition-function-variable-number-parameters\/","title":{"rendered":"TypeScript: type definition for a function with a variable number of parameters"},"content":{"rendered":"<p>In TypeScript, you can define a function with a variable number of arguments like so:<\/p>\n<pre lang=\"javascript\">\ntype FunctionWithArgs = (...args: string[]) => void;\n<\/pre>\n<p>If you want to restrict the number of arguments to specific values, you could also use a union type, like so (similar to Tuples in Scala):<\/p>\n<pre lang=\"javascript\">\ntype F1Arg = (a: string) => void;\ntype F2Arg = (a: string, b: string) => void;\ntype F3Arg = (a: string, b: string, c: string) => void;\n\ntype FunctionWithArgs2 = F1Arg | F2Arg | F3Arg;\n<\/pre>\n<p>This will let the function have 1-3 arguments &#8211; unfortunately the TypeScript compiler doesn&#8217;t seem to like this as a one liner.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Showing how to express variable parameter function arguments in TypeScript<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302,557],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4543"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=4543"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4543\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}