1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

アロー関数のオーバーロード

1
Last updated at Posted at 2026-06-23

結論

  • 交差型でオーバーロードを宣言
  • 関数実装時に関数自体をキャスト
types.d.ts
// 交差型で型を宣言する
export type Fn = 
    & (() => void)
    & ((bol:boolean) => boolean)
    & ((str:string)) => string)
    & ((num:number) => number)
    & ((ary:[])) => [])
index.ts
// [実装]

// 型のインポート
import type { Fn } from "./types.d"

// 関数を Fn でキャストする
const fn:Fn = ((param?:boolean|string|number|[])=>{
    if(typeof param === "boolean"){
        return param;
    }
    if(typeof param === "string"){
        return param;
    }
    if(typeof param === "number"){
        return param;
    }
    If(Array.IsArray(param){
        return param;
    }
    return
}) as Fn // <- ★ ポイント

了

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?