unjs / defu

🌊 Assign default properties recursively
MIT License
1.06k stars 22 forks source link

When a source and a target share a property with different types, it creates union types instead of overwrites with target's type #117

Closed existe-deja closed 10 months ago

existe-deja commented 11 months ago

Environment

defu 6.1.3

Reproduction

live code https://stackblitz.com/edit/typescript-ztqwq7?file=index.ts

import defu from 'defu';

type Source = {
  a: string;
  some: string;
};

type Target = {
  a: number;
};

const source: Source = {
  a: '1',
  some: 'props',
};

const target: Target = {
  a: parseFloat(source.a),
};

// type of merged['a'] is string | number instead of number
const merged = defu(target, source);
console.log(merged);

Describe the bug

I'm not sure if it's a bug or a feature but when both object share the same property, defu merges types instead of overwriting it.

Additional context

No response

Logs

No response

peterroe commented 11 months ago

I think the behavior was expected, and you can get the same result by doing the following:

import type { Defu } from 'defu';
type Options = Defu<Source, [Target]>