vue3 Composition api 变量类型推断问题
資深大佬 : webberone 3
用 vue3 + ts 开发过程中,用 const root = ref(null)创建一个模板引用,编译器会提示创建出来的 root 类型是 Ref(null),但是有的项目提示类型是 Ref(any),这是什么原因呀
大佬有話說 (2)
export declare function ref<T>(value: T): Ref<UnwrapRef<T>>;
export declare function ref<T = any>(): Ref<T | undefined>;
“`
上面是 vue3 ref 的类型声明
如果你在 `tsconfig.json` 开启了 `strictNullChecks ` 则 ts 会区分 null 和 undefined,否则则不区分,而是当成 any 处理
所以:如果你没有开启,那么传了 null 则会匹配到
“` export declare function ref<T extends object>(value: T): T extends Ref ? T : Ref<UnwrapRef<T>>; “`
所以最终为 any