之前我一直理解为 & 是并集(其实是不对的) | 是|| (这个是对的)。
& (Intersection Typs)
Intersection Typs:&是既满足A又满足B 。如果遇到A和B冲突了,那么此类型就属于无效类型。无论变量怎么写ts都会报错。
e.g1 : this is a paradox. cause property can not be string and number, at the same time。so it turn out to be ‘never’
type bbb = {propA: number} & {propA: string}; => {propA: never}
e.g2 : this is a paradox. a wrong example
type aaa = Record<string,never> & {propA: string}; => ❌
const AAA: aaa = { propA: '' };
报错提示如下:
Type '{ propA: string; }' is not assignable to type 'Record<string, never>'.
Property 'propA' is incompatible with index signature.
Type 'string' is not assignable to type 'never'.ts(2322)
| (Union Typs)
这个好理解 就是 或者的关系
tips
Don’t use {}
as a type. {}
actually means “any non-nullish value”.
- If you want a type meaning “any object”, you probably want
Record<string, unknown>
instead. - If you want a type meaning “any value”, you probably want
unknown
instead. - If you want a type meaning “empty object”, you probably want
Record<string, never>
instead.