Type Script 中 如何寫 overload

在TypeScript 中視可以寫 overload

TypeScript 是支援 overload但是非常奇葩 又愛又恨

可以更短的寫出來

但是我覺閱讀性卻欠佳

如下方所表示

Code Like this

import { DateTimeFormatter, LocalDateTime } from "@js-joda/core";

export class DBUtils {



    public static toDateString(inDateStr: string, formate: string): string;
    public static toDateString(inDateStr: string,formate: DateTimeFormatter): string;
    public static toDateString(d: Date): string;
    public static toDateString(arg1: string | Date, arg2?: string | DateTimeFormatter): string {
        if (typeof arg1 === 'string' && typeof arg2 === 'string' ) {
            let ldt: LocalDateTime = LocalDateTime.parse(arg1, DateTimeFormatter.ofPattern(arg2));
            return ldt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        }
        if (typeof arg1 === 'string' &&  arg2 instanceof DateTimeFormatter) {
            let ldt: LocalDateTime = LocalDateTime.parse(arg1, arg2);
            return ldt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        }
        if (arg1 instanceof Date) {
            let dStr = arg1.toISOString();
            return this.toDateString(dStr, DateTimeFormatter.ISO_INSTANT);
        }
        throw new Error("not support this!!!");
    }



}

留言

  1. Water Hack Burns 2 lb of Fat OVERNIGHT

    Over 160 000 men and women are losing weight with a easy and SECRET "water hack" to lose 2lbs every night as they sleep.

    It's effective and works on everybody.

    Here's how to do it yourself:

    1) Go get a glass and fill it up with water half the way

    2) And then learn this crazy HACK

    you'll become 2lbs lighter in the morning!

    回覆刪除

張貼留言

這個網誌中的熱門文章

angular 如何Http 如何設定 CORS (Cross-Origin Resource Sharing)

Google Map 單車路徑計算坡度和角度小工具

Google URL Shortener API 快速教學