TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Type Polymorphic Functions in TypeScript

37 pointsby CapnCrunchieabout 3 years ago

5 comments

seniorsassycatabout 3 years ago
Can function overloads be marked private or deprecated independently?<p>Does the implementation need to repeat the effective union type?
评论 #30700294 未加载
评论 #30704438 未加载
lloydatkinsonabout 3 years ago
I always really enjoy his articles, I wish he had a newsletter so I could know when a new one is out.
iron4wineabout 3 years ago
great content on TypeScript as usual!
gwillabout 3 years ago
given that amount of work(and maybe i&#x27;m just being lazy), wouldn&#x27;t you prefer a unit test?
评论 #30694948 未加载
评论 #30699740 未加载
评论 #30697859 未加载
galaxyLogicabout 3 years ago
I&#x27;m not sure if it is a good idea for a function to do different things based on the type of its argument. When I read code and see function call I would like to know what it does. Now if it can do different things based on the type of argument I pass to it, I can&#x27;t easily tell what it does, especially when the argument I pass to it may be my argument, or may be the result of some other function I call.<p>A more readable solution I think is to define multiple functions whose names differ by a suffix that indicates argument type.<p>Arguments with default values are not really &quot;polymorphic&quot;. They juts make it possible to call the function with fewer arguments.<p>And instead of &quot;polymorphic functions&quot; why not use ES6 classes which are polymorphic by their nature. The class of the recipient determines which method gets executed. There too you can use the approach of defining multiple similar methods whose name differs by a suffix indicating their argument-type.
评论 #30694944 未加载
评论 #30695246 未加载
评论 #30695460 未加载