如何编写健壮的TypeScript库?

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当你用TypeScript编写库时,你通常不知道这个库最终将如何被使用。即使你"},{"type":"link","attrs":{"href":"https:\/\/github.com\/true-myth\/true-myth#design-philosophy","title":"","type":null},"content":[{"type":"text","text":"警告"}]},{"type":"text","text":"潜在用户,你编写这个库只是针对TypeScript用户,你还是可能会在某个时刻拥有JavaScript用户——或者是因为他们不顾你的警告而使用这个库,或者是他们因为"},{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/Transitive_dependency","title":"","type":null},"content":[{"type":"text","text":"传递性依赖"}]},{"type":"text","text":"而使用这个库。这有一个非常重要的后果:你必须将这个库设计成任何语言的开发者都可以使用!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其主要部分是函数定义和函数体。如果你针对一个纯TypeScript读者编写,那么你只需定义函数类型并信任编译器处理其它事情。如果你针对一个纯JavaScrpit读者编写,那么你需要记录那些类型,但在函数中将实际的类型设为"},{"type":"codeinline","content":[{"type":"text","text":"unknown"}]},{"type":"text","text":" 并检查调用方传递的内容。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如,给定如下代码——"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"interface Person {\n age: number;\n name?: string;\n}\n\n\nfunction describe(person: Person): string {\n let name = person.name ?? 'someone';\n return `${name} is ${person.age} years old!`;\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一个JS用户可能用任何东西来调用 "},{"type":"codeinline","content":[{"type":"text","text":"describe"}]},{"type":"text","text":" 函数。正确写法:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"describe({ name: \"chris\" })"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 灾难性的错误 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"describe(\"potato\");"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 当然还有我们最喜欢犯的JS错误:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"describe(undefined);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你的库的JS用户并不是故意这么做的。恰恰相反!在任何足够大的系统中,很"},{"type":"text","marks":[{"type":"italic"}],"text":"容易"},{"type":"text","text":"将错误的参数传递给系统中的某个函数。这通常是一个很难避免的错误,比如在一个点上做了修改,许多其它地方需要更新,但漏掉了一个点。故意的JS开发者会把坏数据发送到你设计精美的TS API中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"text","text":"如果你针对一个纯TypeScript读者编写,那么你只需定义函数类型并信任编译器处理其它事情"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章