xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

TypeScript Intersection Types All In One

TypeScript Intersection Types All In One

交叉类型(类型合并)

interface A { a: number }  
interface B { b: number }

var ab: A & B = { a: 1, b: 1 };  
var a: A = ab; 
// A & B assignable to A  
var b: B = ab;
// A & B assignable to B



interface X { p: A }  
interface Y { p: B }

var xy: X & Y = { p: ab };
// X & Y has property p of type A & B



type F1 = (a: string, b: string) => void;  
type F2 = (a: number, b: number) => void;

var f: F1 & F2 = (a: string | number, b: string | number) => { };  

f("hello", "world");  
// Ok  
f(1, 2); 
// Ok  
f(1, "test"); 
// Error

refs

https://github.com/microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md#35-intersection-types



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-02-07 16:52  xgqfrms  阅读(39)  评论(0编辑  收藏  举报