泛型

软件工程的一个主要部分是构建组件,这些组件不仅具有定义明确且一致的 API,而且还可以重用。能够处理今天和明天的数据的组件将为您提供构建大型软件系统的最灵活的能力。

在 C# 和 Java 等语言中,工具箱中用于创建可重用组件的主要工具之一是泛型,也就是说,能够创建一个可以在多种类型而不是单一类型上工作的组件。这允许用户使用这些组件并使用他们自己的类型。

你好泛型世界

首先,让我们做一下泛型的 "hello world":恒等函数。恒等函数是一个函数,它将返回传入的任何内容。您可以将其视为与 echo 命令类似的方式。

如果没有泛型,我们要么必须给标识函数一个特定的类型:

function identity(arg: number): number {
  return arg;
}

或者,我们可以使用 any 类型来描述恒等函数:

function identity(arg: any): any {
  return arg;
}

BTFIuscf6mMGN/FhwYEsGwzyRgKp4otybpVJqB/SeS8eM1oYqh0NHmuWxeZxYC8UKF79nbx5cqGOxl3NW2Zun9kd5PvLXQ50B2lV5cKJyIVsvZFTEFj//KarSOoBl5nEP56Q2rNzK+IqZJkVuayLc3JdQJ3wsqX5Bd8N2NwFBQHl53++WWT2+czHMZD96n0dCDdw1Xd1NBTRd96KmcYFeJ6kv06TS5uY7uNVdvgAI042l6ZAVU/DL0NCpPeBH8nFDxo171uwiP8i9juwhXi353esZLoChsin5zEeU1IXs8FEaM4qAc60BYdxLbwWGK6NQrpyXTokQatuecPIe9AUMTc392FL2D7Sdw4nSplhGOVsrvHZcHwTLirbyiw5V2VTU4l4+AJzEjYoREU3whOccg==

xscIFB2MFA+c7R9dJwBgFkAXzSs5LpOrypweo5oqgbY99llSaHxxTdLZrgzncxo41km0uGkCPusXzkomvXd7D1naZW+AIcZq6KlRgcnhSzvjkE8LgWpR7CRX+lLx+s7yxVIFcsLMQdUSKWEw8F8fn+b8OGr+hS7/uF4Nr+ZvzOaYq9jEcQ5ZzWmQ7hAfzrbbrlyVIS46VzQY2BbpjmQZJjggziYDo/jKSIZjmiboKeh+eBcsX0920s3XTg7L5qo2aC//lU3PTtO54bvCTgGpagOe5XQwNoxnsL/gPLKhscekgFUjPSqy/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Qg+Pcg7ml4kgYOmp344llv//BilT48IqsCsZLUo6zvnoxZkKeKWeaqm+onA59WPlcly9CQFgZjl/0+uUOmkPt3t6C/J1RnBm6U4x4vcaThffTHjBmFI4F0IapJ5MPjAwugupyDmVc3m6ulxJGZWXrtNchBZUSi6ZdW3zi4EIzrMrdKdhQR6bIkm81AaC+76/7gDuN1k7JfKcmk=

function identity<Type>(arg: Type): Type {
  return arg;
}
let output = identity<string>("myString");

/zacUnvCJodbw97AcHI7gywgI25RRC/w6C0cRJc0YNpeUKmzvGylnPYHbFvZlbh4lJCmUFP2DpaS0Z7ovIQe7RqRYn0mi/KpoQQH1mqHrOiXuLPb3jQUCtuJfdydDp1vYXsFy1GjvBds/Dd9Qj7+npw08lyvLJnTZxwx8RgoJnffcmHeMumWZV6QKQnclSNU4ZdmOKrv0niI1UNwKd61q8yEDRARMzbsj6TQkk7RGaFH+Gx2KTnfjDcYNVa2Os3a

zeyGXLvnEyeX7kzDlUwqgoAIDIosGbmzm7BSiUFt0YA+acWPLbNjuWQVbCIWFzoT4cjxtsOIYWhXiBSAy7LEb9ZqmTeFWeQopdjX/mt07V0cIXJgxtk797ZNsrO5deFfdfHDIhuVTWoPtEF3478/oGah5c1Ze/I1jijY36l/go7GgtZCRGrwGTIs4mDNi4OjxCMraOF+Ph+u8kmQRj+K606xO4DyuYhr8vBvYADDqV2UPUWjFM4PybfL25vgSu+dGnJHFwwfFOIapa28tbFm0A==

function identity<Type>(arg: Type): Type {
  return arg;
}
let output = identity("myString");

HfvhnRV2TSKOWtyyyh4TuO99i78vAQiXh8BUt3NW2ZoX5BUCKV1L0WD2dNRYayEWzOe/DTifC4dkHQUayZ/QJ4PkPT5sT5ukmtKmG9WmtAJ6GaNTZNAGTdmsw4nABfalziVzGpIGq6n+vkxNBb0sbLJ9Dcu4C8kYkEvAQIc6wtxzkzc2YQ0AjM6u8By5LJKLrZFF6i8ho+5pwGaHrDg6e6NGHuWBmQpVu0/xo6hJNYasteUaI3C6NDVtA/LWjoDJ1sBqA3NVYdeVfTVUesXY/Ve+1oNHhEfUvPzInvb/YPykgz1JL40plZsE5kv9v2W5jLk3m2c2pcdwZSURRb2fWJNG18uL7+T1gOcVuE8AMdY385Sy3KR6Dsc9hfExcUNnmgChf6pbjMQiZopEyc9Ft9fKJ4MCTc3QG4DWibdmkKSs9SyyQPX0+rjVkCbzeUy/Tx4WHoUDHYuzADi0YjSd9CdL1FNwVXBItYQ3dbcibIgTHEvBfmrXweRHRblxzspQDqqTCehMvjEi0wZR5dgvM/mP95XSqRfNzG55ayvsM5t40yHdwmTKizMTfi0P7Mw3pSNYol85ID5R78pAt2mK/w==

使用泛型类型变量

tQbFgaZW7CwQIlOAPiKivAgFiqg9GM4+4aS83Wm4Wsm/5Np/yVFuNN+yqfxcaJy+xuDWb6OrF/V0z817m1r7/3u+zRHsOdsZXsl9eZsH0TzTeBn+a4l0t5ksOc4eTLAQz7ImERhZrGsBra3WWE55oliwa0SGoUkRbkTjtE1fZZumGHwh27rYm2XNw07k0fOcf1/94U0gzSV6w4rov6dE486dM1JRMlPca6Ue1LX2UelSx68OG0hKtR8XIM57SxlRSI97EG2/uqLLdQIy5R9uOnJAsS+uPtZ6Y/WibR6Tu4KjmalcwDFsg9+w67e+HdGrZxG1T+atUQea5H4PWtouiXy0NsE5ADtm4v/BZOovMs0=

1g/pgYcDDnoTRLLOXQkXK4+L7407gnfBY72mCG28WmQCIYdYbvvCxXHEM6ajSJBBQGRzz29fOlyF72mi9CmnkQ==

function identity<Type>(arg: Type): Type {
  return arg;
}

d0eNga02Xi0CFCtY46ri3kpILmBe5NgAgPSjoqnpNh7oXjx9/nJLrn91io2g3twiKGN99botk0IIxmmSyw135UaHRPjXi7n5kePx4YHs21dRM4D0tRT+bRjhR7aDIwgRQSOuUh5p6If+v66MBrFCNl5/jTr48Jy0xA/+Gw+zIliG7hWTkkRmj2XrxWv7JiRg

function loggingIdentity<Type>(arg: Type): Type {
  console.log(arg.length);
  return arg;
}

EObJ5Vb2vEJjY+xP7QgsGzq5PodtUZk6P/JRkId+jbUTdmQhMfx+4RJxMth/mJ9iWF7n28Rb74DgFlsrJb2io4CRmBI5x0k2vavkdRXNRmb386wa71MvDoa0x2zAWXfKJoLIPFealu02gmnM1R/WEi9152R49Fot8c7DOrbobGKkm0DKy6SUOmnkS/OUjtVs6+vlRZdWAVCnRKIjyJZ48VIlwmYTuTxAgij66qLJ4uxYqJWWhfyCYpB6C2/SjdtNoqhpo6h5c9VcvKH5Y9XYm9kGeCdo99hLETu30mU53j1pOKJoMfrCVRxMhjwSsrf3G1pWmwmmGayqj1lJxOwi/6xWj93jNN9dVI2wMl52C1I07TTUfBkDoDmOulljB3hGFTlnfsXmIn2wTNVNTuXUISfHUs9RlOtF7d6h6S/vtl98y3qfcn5qtE5GoQgBmQ53xmce1WpCEQ5+M6EFfK0D+NidQK6DQ+CKidSuWzxDvZtAaO2JWrpygIOoD21APxMX

w21EsACV4gT7Oh07Qa0yELwvwPeJUo53fS0NlHzYqeBiARMUN+XePGndMEnxk8A7YBXKN77CTOzb5QxOxGO51tZOsucP71nBdeQbikpR2DmaAm78W4vxlvBuCAd3k4PO6PWy5K9bu04BOsLai6/qZz/1SrdLghnnHYUG3wF2I3JhyS+o8ExPNjo4JRr1aM8dPrO0dIYXzH88GeX9qR/M/jLUqprIzx9LNhcX+Dhk/mfofyi1T0YFA6+1HpzV6b5gKkaJ4e1S2Ie/lJsSACHFfbyI/v0Do1Fmlci8og3mScMo5byRT5l2KKS/A4wDJmJb6yqIMrYsh1vFqsxmdoU+7sX3zc24HUbR7WOO3E/9rwPpD3K+dq8PTaoeUVeYatSj

function loggingIdentity<Type>(arg: Type[]): Type[] {
  console.log(arg.length);
  return arg;
}

QyNWUCxl35UZVvogv32gmkkiMYV7+dNZscC4AYhjY90bICgNs4G+6V42FDgVhBRTQJaIbxA0HQiIId/fKkHCIyetIkRLWQc8Jaie3n8NjAiUu3Dsm7QTr3iilnnD5Q5S9Rs47ykMuy6ar8hs2150broz4mY8FTqUAp61F6YSeLTtYP6KB32BBUajTLIq7kBJDoTnGH8WAupLzaR6fwrBX6OL2WJsVGcm3plSkmTybOjuax5qfKonKQXLR+78KoWPJnnSAlBtR35qV6xcIYNIGLvF95agZ8bruaIbP7Vct+s3B0I2/6NHymxMVPk/PQc7KcxbBFHpfkLcKkpWWRGhmzM0vrMDUMy8qqdJPjkd38eprqA9PVpoGMsL+sDCFOIymIGUMu8JqLjoaEnvMQ6105h9JraabhTZ/pLl2vucH14SaGA3EWnzvmiWr3f68Zt6xxCvAix//QwAru68oORYa5sS8XP6nF0y0fE+htEYPgMaMkKw3Tvy8i4s1K7PtFtwmY2taQ19180IWpYmJLQol2mcL6ZI4Qf11VrlmKAJKUX+8s0iYMWBRURoMN0uGim5Y99RxdMPl1P/EyHQs/mw+26zMTxo1BTpurKzdnFaF3DFYtWWzH2WPmQDFzLqtSAgWmAuprbqPcs+GOPxFXA3zSbO7q0zzkvqLruIpXgjjIzZ/f8mG6NoGfS5S2/ZtKY81fOnngM1g716CZxAMt24XqhtvcQgnfhQGt8aag+9LuP/lhsNSrNw8wCFZCamaw5AQWY6Fi46yeILrfAoVDEleQ==

iqgKCJWoxRJa4mPL/lN9juEaOWTU0/O5O2+vJ3jOPmQnrhokF5IILP035HFmXxva

function loggingIdentity<Type>(arg: Array<Type>): Array<Type> {
  console.log(arg.length); // Array has a .length, so no more error
  return arg;
}

w4D6IlLp80rCdjZTvmI7lT018K1f5SrSG0E2IKomUNJi4ay5uswH5SwI6RPp/760BwY1r4a9AGTd+4CTQIK9KSDfVCWZygwZbDt607Kr/R7j+xP2T3ybLm+DEcWwv710sUmWpw6C0YZhPWaGJN9J3s7aXRSpropfcNmqvokY0k+sVFopCfMW0GDkzDw4atzTGoRiIu0/5DoDqbEsQ1senS+WEdbDkmWVBKPnla/vdv0=

泛型类型

Ctcw+D7HqvqS8ZVY8BCZvB4b/VTiMMCh2KA4BvF8yoEHonZwBxgPx+dxlg+Kb53D0YjEK9bXoIOei5ROg75Yb+PRNSzFzKBIhtxx+yzXXAvuap2Pu0bfteHR7l93/KuPFxas5wtyIVfrOwo3lmm9az1Rp2Yhw/2USwPa3wnUvVKAN5airjvLGe+h6s7wct3zInDK8ZKvZdEHkvqhw21REp3bFcFItjkinjOsvNqOMfM=

RbK8O1Jv9/DZS9rq+kFTrbh+y8wicesULrApEqpPj8Xt1mQNsKBTToS/ixdJd76cn+bH05dGgnQxFVNLXI01SC0CmuD49eOt5vi02KLFVJb3IeG1oOjC2btDt/oanZcG9zRvSI3jAC+MUAX5dQljLw==

function identity<Type>(arg: Type): Type {
  return arg;
}

let myIdentity: <Type>(arg: Type) => Type = identity;

Qt86TAq1qgD0SxzQEmvMYo2XdUKAJuJzfW08oiOB+r/4X4387DAVsCd/rC7NjCJ9hG9ef6IYKPSco42dp5RuctqsfUHqhgE7xrgtlgzzME9gQo4mhWGmlukHIRR+8p6KI17TWGTuq4L6HXPyTY7/m6ap3LmT2mqfegvws8F2jZtIePPzXVmsdfcDX1gBcHRG

function identity<Type>(arg: Type): Type {
  return arg;
}

let myIdentity: <Input>(arg: Input) => Input = identity;

3cDL4ptFkUaUfKE2L+7W1GBqMppp4beRhykLnN/HzjxSBXTaQDyV5lPY8n6f+Qb18I3aDTvtj6CCITLoJUD5FoUH6fUHaIzWoUhrxcanYnk=

function identity<Type>(arg: Type): Type {
  return arg;
}

let myIdentity: { <Type>(arg: Type): Type } = identity;

8tJ3KIZsBfFIx7MPnviO2Wu5KSOl20OdqgyeQqaHvPlzhzwwbTulgiMDAS0WsmbUzM2620MbO2lLm0i5/4Ho8LNKdxT+J29O69X+4TLDc33I6AxFww3NqnZdlJrDdF6g0xE/dG5fvm8JvnJ8BF4Hpj/Pmywb+j56IP2Pt8wh2OhmP3cqVSE/c0f/2WBvf9h5

interface GenericIdentityFn {
  <Type>(arg: Type): Type;
}

function identity<Type>(arg: Type): Type {
  return arg;
}

let myIdentity: GenericIdentityFn = identity;

aMjetV+zGKQ8anuC5YZtcPhVvkakUn95S6FH2o29ZN8Rw46oLdJ/N6A6hxL7EThzGtWRN36j/7QT1Wz7U1VLcmO0Jf4sSibr0LuWq0G6Vk76rXFAwYaTCw51cYQeFscGX/PNMkKG3vQ1gKrJdwoNgPGQuYOpYjYlnTfYQkuq/xzqIUFgLhw0Us8fFGgZ//Ezcpl+4qs3YaAAFxfJlVTd6O4sehFi820H7TqCwXF+4K35CO6y73WAoCxnP0exZhuO289t6+mKrT33v8aLfLjPlHN5PHq2xwi61RcQAtmpiUNbRPStoKY7l0pPypSbhU5BvhckHBXWJq1GrbO4c+VF1ZiDvspUGA6dbtkmuHaJQAQFKWoILFmwi+AOPqKFLiuYLVc+hlXn9CWENRociJlo8g==

interface GenericIdentityFn<Type> {
  (arg: Type): Type;
}

function identity<Type>(arg: Type): Type {
  return arg;
}

let myIdentity: GenericIdentityFn<number> = identity;

HfvhnRV2TSKOWtyyyh4TuA2nQRJpqmkvuMWHuI/ZwxdNFvTxtmyM7n7rtIbz2XNddL5kmuFy9NE6CUeCmAj9OTdkVEiVwZVZsdSwWM7ZCEAwr42aqe6ExHvMBragnxHRbqklQX5dylBnh22dio4EbRoLtEkYQHXj/xhYoWXaDLUAPKZtTvA7SvwYA+FV/Jhm3WpEhsZ2J77eP+k1CBuBqxNLjiYu8OQSEa1shik0k30p0k/utxRMG5EPoecA1rJeBb2eoXtE5xWoLvyUK8T/eWMp72L2Aw8OThWTmWgXuyibEHRRJbbKLFIj2JHSuXjg0Du4YZIbOoh7AU9BxxBrGDcIDmFnnHeEkX402uh9p1GDVKY10SZPog2eOnfgQTHvUaPBT6flBj2R5kOu//LgBNL8ZiK7jINDTN21gUdHU4eB+aszx7PLCVH81vNYorJ/9s8nMdmEJ+pO6k9zzPQ2+QrN28mbgPIpWB2Gw9UVXhrzF/gbCOBNWnVKqdPHE4jG7F5GjZha3kkcQnyB77pRgdw0VClu7OmKYBYX5PZO8vBz+1D9CcGzhnyUQsgjPA7epFtUYiBm+QOK6mqv3OkjMZTWPWr3u/0oBQenZiCGUvvHzKI5+VWjNh1WslaG5lSqfiLgPl89oQx+LZlF0eBjULwtjFZwNmigfpXTAoxHXII=

v9to96gnZtAzDCddfEWVjEHnhk7OaD7kwBA8nyflCW2M5xmQowIpO4htkAkal5yQOmD4OkZreinvpQ+w+lqWGHA7A7zp+LHY597lgHPNASnFMONkaA8cLaZ3kgBtIk4Hcf/Uz6reoWntgcoLaxq3VA==

泛型类

lmebQSw5gT41XBz3ZCN+aBP9x2MDCWtjnoG+aPGUk+eF5fPh90jkQe/Kxkes418PtZhiBLBaHVJpA1bKryrz28EToid5FMeGjpHDjZNqOeY2VJvMWDeSMPS39xXBgJyTJu/Bc+EX7tvvPU0jfIMXDuI/yz23lmoVBl39UkheJ3wHWqlZMZrJZ8b6GVscucC4JI89mkisKcvU41qTBzz4sA==

class GenericNumber<NumType> {
  zeroValue: NumType;
  add: (x: NumType, y: NumType) => NumType;
}

let myGenericNumber = new GenericNumber<number>();
myGenericNumber.zeroValue = 0;
myGenericNumber.add = function (x, y) {
  return x + y;
};

ZLG2xVcs9i/w+/Dat2aF71vEGjtGFEg3srvgiV8kbreGQEGTCmnmvmxfEABgtVCk7pi5vxmhLMbkzX8RvWfBpz1QjJu55ejHIpou4CRgaWhDdz0a/ycSZEyXAEOT2Q8nbXiJoAVY+ME+KIo/cx6EpYo2lk0ZwLNvL3UN5h4OLK62gEkvFJwM501wtLju1Ed22X/PI3gSSCw9GG70Fk+s7lJwn3jx1PAOI3gq982NSgtSn9p1fiuV9cyICKdm86PihNiVPTBlew5p1cxCbGafZbUqYCnj4Q/JU7ywnUPAm+M=

class GenericNumber<NumType> {
  zeroValue: NumType;
  add: (x: NumType, y: NumType) => NumType;
}
let stringNumeric = new GenericNumber<string>();
stringNumeric.zeroValue = "";
stringNumeric.add = function (x, y) {
  return x + y;
};

console.log(stringNumeric.add(stringNumeric.zeroValue, "test"));

WT/a5aGT1ROHl1r4pU3cGGAJ/vr+mF1KX5ChXqCfkrRmHDYzyr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QhyJeQBu1QpPTUxM=

function loggingIdentity<Type>(arg: Type): Type {
  console.log(arg.length);
  return arg;
}

yijNvkTIbWHQyQonEWQKEfmHBfT22p2/ccKqNqMFhs58a2dVInYwtplXlVBWoHZ0y8HP6QqVRa0UZg8Cfzj4QLugNlnsJmX/ueiRcdR9uN27IzOn01E93363LfWNFEVY9UDNUXfZog81Tb41ZeNhOOxJKIEVfgLkgfRKZph6jJb5ZZxhCy4T+GDo1GQLA+hlIy+QwWUnuwNdgPR78Ng48VyG7Yec6aCPeuhEOXvQbge4Z9zSSvN0phSKgcE8ChItvu9/xmF5/5zDCOnKLSIjQNbYS16g0QcfPHnMgQ5WgHzAhnj6dLVyfB33wHZac2h14DR6Ba/kDwa5Vl2wv2s7pMttg+idFJOlm0qPIFFJi6XFXlMaTProkFm3ep1nnMYfw2B0RH8mNIjjPX3hTq54pqlSC5WNtpmzAChWRl6uf9U=

Ua8x9Oy7bGbXp1OvJoitB/DfwZdDVX4KOupcWe7AMeRq6TVQJ59qXK+p+xFhjfIy/AZqmPHDn6/KhqJqDsAl4wBBPa17Ug5eq5NhL1rSR5+nV9dN32/WwS48D8BYv87vwKzKSjimjuY/TrNrO9IMNt5C774yNDgF1prp4CikRFIsHaJAfp/KjvOV4DuvWu4vGC91cHgIXV+DUasFUta7fTfbHgmyF45wTUAuLO7BoARLciOkbmH1La9H5KRzYIQx5VpTg7rBmp012C+xCJQU/fusb2kwqOfKVvT5/GD4xJU4pdlrol5GJMYty4IP8Yem

interface Lengthwise {
  length: number;
}

function loggingIdentity<Type extends Lengthwise>(arg: Type): Type {
  console.log(arg.length); // Now we know it has a .length property, so no more error
  return arg;
}

3U48X/WR/X4XqBXwMt8UCopGuRFtRTexmtGcgSauJUWKhCOp3oTd+qYmUpxACi4nqiOiRGszG6nUTLEZn1LuR/x+C2Xb36y65ZdtbEi1P0c3pAjXZWgpkVBW1OwIqHT2

interface Lengthwise {
  length: number;
}

function loggingIdentity<Type extends Lengthwise>(arg: Type): Type {
  console.log(arg.length);
  return arg;
}
loggingIdentity(3);

xscIFB2MFA+c7R9dJwBgFl0Bh7uK1zibA37i+9au+0EtgGdjc3x/pr5DF1OY6Qm4JyJzFqbaIWZd+i5X2OO90AjLQEW3UrUZSKXrhLCF7Mc=

interface Lengthwise {
  length: number;
}

function loggingIdentity<Type extends Lengthwise>(arg: Type): Type {
  console.log(arg.length);
  return arg;
}
loggingIdentity({ length: 10, value: 3 });

在泛型约束中使用类型参数

Pg/D+HUdyuayTwzAP1br1Hs/tBQ63smU9YcwTWaepW4Zv5nvFtwhVjb6wSFMCzIEz/QSaMzRPbQFOJL5NneyhL0LHsppmVjFq9iKLynJWEbJuvbD2wDLGsa4jUJlJQ2GqPBBfS5QoGAECqjkXk2I3QLRg6Wm/uB5ijSUOoxc8Khc21jIFCCVzZ/zeKVjCgjm0X7xmQ6oyL923RonaZqVOherYe71732s/09VNRJB+HGZglme/+PQ2HByrP5AwTjeTCyBhMUMjtEamANdqJw1aeKjNXuDrs7V6CGFn4W0q0RNSEoYJ/fHDgHUThLftpqBc1kQuE2gVGGyweuAvftLSJ1z9rzGvkiZHYN7FQZvcC928D0mKfUzwT+8MnRhHFgN

function getProperty<Type, Key extends keyof Type>(obj: Type, key: Key) {
  return obj[key];
}

let x = { a: 1, b: 2, c: 3, d: 4 };

getProperty(x, "a");
getProperty(x, "m");

在泛型中使用类类型

Rb6cUIfR1jf5G/PcTj9e32UlCI27CF5R/fstzBZXe1y6L+sgJStbExMP6Tfz9KU0mBuAFAqNxmzXWNpVj/deaztxpFsh9uOByfzUallS2UFsw5Wm03TKFVbsIWIyYZ+ZrlE4zc3Y5Q5QqxkqPyWEUw==

function create<Type>(c: { new (): Type }): Type {
  return new c();
}

DyRwgAGlSze8MBCDxUvzP90S6GKHFQBvSjmDMSIRhBev6DXYltz7RPL/4J+boSE45aWWwycG0LOQjn3TBu58MLdGfpgPDOoqNN0OIvETuq29FmTZAinSt8A8oLYnqaUrkqSC9Oy1FfIYvzUYXUJgYoxy5KHG3mWfNEltMJudgpk=

class BeeKeeper {
  hasMask: boolean = true;
}

class ZooKeeper {
  nametag: string = "Mikle";
}

class Animal {
  numLegs: number = 4;
}

class Bee extends Animal {
  keeper: BeeKeeper = new BeeKeeper();
}

class Lion extends Animal {
  keeper: ZooKeeper = new ZooKeeper();
}

function createInstance<A extends Animal>(c: new () => A): A {
  return new c();
}

createInstance(Lion).keeper.nametag;
createInstance(Bee).keeper.hasMask;

7GWMZgxm1a3j78wHj1pBo7/l7AO6jPqSAtiLz/y+PshD7tY6GCYP9NoD3z2bBjiDE5CD2kAWqIM0VZtnA/hYPWJ9JMI8h2QbjPAn/GFS6ht2faFwqoKMtNSLLKUDp7LRywUlVActNMzkPvqmPLmAYw==