core icon indicating copy to clipboard operation
core copied to clipboard

types: improve readability of built-in type

Open Mini-ghost opened this issue 2 years ago • 5 comments

Description

We could write this in code, but the types would be unwrapped:

const date = ref(new Date())

We can declare types that should avoid reference unwrapping by writing:

declare module '@vue/reactivity' {
    Export interface RefUnwrapBailTypes {
        Date: Date;
    }
}

But Date is a commonly used built-in type, would it be better if we support it directly?

Before

const date: Ref<{
    toString: () => string;
    toDateString: () => string;
    toTimeString: () => string;
    toLocaleString: {
        (): string;
        (locales?: string | string[] | undefined, options?: Intl.DateTimeFormatOptions | undefined): string;
        (locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions | undefined): string;
    };
    ... 39 more ...;
    [Symbol.toPrimitive]: {
        ...;
    };
}>

After

const date: Ref<Date>

Mini-ghost avatar Sep 04 '23 18:09 Mini-ghost

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 86.9 kB 33.1 kB 29.9 kB
vue.global.prod.js 133 kB 49.9 kB 44.7 kB

Usages

Name Size Gzip Brotli
createApp 48.3 kB 19 kB 17.3 kB
createSSRApp 51.5 kB 20.3 kB 18.5 kB
defineCustomElement 50.7 kB 19.8 kB 18.1 kB
overall 61.7 kB 23.9 kB 21.7 kB

github-actions[bot] avatar Sep 04 '23 18:09 github-actions[bot]

I deleted BaseTypes because its definition overlaps with Builtin.

Mini-ghost avatar Sep 05 '23 12:09 Mini-ghost

CodSpeed Performance Report

Merging #9129 will improve performances by 63.31%

Comparing Mini-ghost:type/support-date-type (57dc127) with main (9d1ca32)

Summary

⚡ 1 improvements ✅ 52 untouched benchmarks

Benchmarks breakdown

Benchmark main Mini-ghost:type/support-date-type Change
âš¡ reduce *readonly* array, 10 elements 1.8 ms 1.1 ms +63.31%

codspeed-hq[bot] avatar Dec 11 '23 10:12 codspeed-hq[bot]

I think you misunderstood what I meant - BuiltIn should include Primitive. This PR shouldn't alter how Builtin works.

That said my last requested change seems unnecessary, since UnwrapRefSimple is only checking for object / collection types so your previous implementation was actually ok and we should revert to that one. Sorry for the confusion!

yyx990803 avatar Dec 11 '23 14:12 yyx990803

Understood! I will revert back to the previous commit.

Mini-ghost avatar Dec 11 '23 14:12 Mini-ghost