当前位置:首页 >> 娱乐
娱乐

七爪源码:基本型城外

2025-09-20 12:18

- value: Error }; interface Warning { text: string; }

今日我们的示例再加了。

const formatErrorMessage = (value: string | Error | Warning): string => { const prefix = 'Error: '; // If it's a string, return the string with the prefix if (typeof value === 'string') { return prefix + value; // <- value: string } // If it's an Error, return the Error.message with the prefix return prefix + value.message; // <- value: Error | Warning }; interface Warning { text: string; }

此前,我们的 value 变量仅仅是 if 解释器后来的 Error 示例。

const formatErrorMessage = (value: string | Error): string => { const prefix = 'Error: '; // If it's a string, return the string with the prefix if (typeof value === 'string') { return prefix + value; // <- value: string } // If it's an Error, return the Error.message with the prefix return prefix + value.message; // <- value: Error }; interface Warning { text: string; }

但今日,它也许是正确 | 发出和 .message 也就是说在发出此前不实际上。

const formatErrorMessage = (value: string | Error | Warning): string => { const prefix = 'Error: '; // If it's a string, return the string with the prefix if (typeof value === 'string') { return prefix + value; // <- value: string } // If it's an Error, return the Error.message with the prefix return prefix + value.message; // <- value: Error | Warning }; interface Warning { text: string; }

typeof 字符串在这里对我们一定会有试图,因为 typeof 参数对于这两种情况都是“取向”。

const formatErrorMessage = (value: string | Error | Warning): string => { const prefix = 'Error: '; // If it's a string, return the string with the prefix if (typeof value === 'string') { return prefix + value // <- value: string } // If it's a Warning, return the Warning.text with the prefix if (???) { return prefix + value.text } // If it's an Error, return the Error.message with the prefix return prefix + value.message // <- value: Error | Warning } interface Warning { text: string }

在 JavaScript 此前处理方式为这种情况的则有方法有之一是检查和 value 到底具备 .text 也就是说。 如果是这样,那就是发出。 我们可以用作 in 操作员驻守来明白这一点。

const formatErrorMessage = (value: string | Error | Warning): string => { const prefix = 'Error: '; // If it's a string, return the string with the prefix if (typeof value === 'string') { return prefix + value; // <- value: string } // If it's a Warning, return the Warning.text with the prefix if ('text' in value) { return prefix + value.text; // <- value: Warning } // If it's an Error, return the Error.message with the prefix return prefix + value.message; // <- value: Error }; interface Warning { text: string; }

如果给定取向具备给定也就是说,则此字符串回到 true。 在这种前提,如果 value 具备 .text 也就是说。

TypeScript 明白我们的 if 解释器只有在 value 是一个发出曾一度为;也,因为这是唯一也许的 value 具备叫作 .text 的也就是说的种类,因此它将种类缩减到 if 块内的发出。

在第一个 if 解释器后来,参数可以是 Warning | 正确。 在第二个 if 解释器后来,仅仅是 Error。

平等抬高

支持者可选人参数也很类似于,这;也定必需 value 为 null 或 undefined。

const formatErrorMessage = (value: null | undefined | string | Error | Warning): string => { const prefix = 'Error: '; // If it's null or undefined, return "Unknown" with the prefix if (???) { return prefix + 'Unknown' } // If it's a string, return the string with the prefix if (typeof value === 'string') { return prefix + value // <- value: string } // If it's a Warning, return the Warning.text with the prefix if ('text' in value) { return prefix + value.text // <- value: Warning } // If it's an Error, return the Error.message with the prefix return prefix + value.message // <- value: Error } interface Warning { text: string }

我们可以用作 typeof 字符串来处理方式为未概念的情况,但它痛楚用于 null。

跑去说一句,如果您只想明白为什么它痛楚用于 null 以及 null 和 undefined 错综复杂的区别。 我有一篇比起概要且内容丰富的篇文章辩解顺利进行了理解。 我会在注解此前留下一个客户端。

对于 null 和 undefined,我们可以花钱的是用作相等字符串,例如 ===:

const formatErrorMessage = ( value: null | undefined | string | Error | Warning ): string => { const prefix = 'Error: '; // If it's null or undefined, return "Unknown" with the prefix if (value === null || value === undefined) { return prefix + 'Unknown'; } // If it's a string, return the string with the prefix if (typeof value === 'string') { return prefix + value; } // If it's a Warning, return the Warning.text with the prefix if ('text' in value) { return prefix + value.text; } // If it's an Error, return the Error.message with the prefix return prefix + value.message; }; interface Warning { text: string; }

我们的 if 解释器只有在 value 相等 null 或 undefined 曾一度为;也,因此 TypeScript 将我们的种类缩减为 null | 不恰当的。

这称为表达式缩减,它也适用于其他比起字符串,例如:

不相等!== 不稳定的相等 == 不稳定的不相等!=

确实缩减

但好事就是这样。 表达式缩减不是 JavaScript 检查和 null 的则有方式为 | 不恰当的。 这样花钱的则有方法有是检查和该参数到底为;也。

我有一篇概要的篇文章理解了 JavaScript 此前什么是;也;也。 我会把客户端放入注解此前。 如果你能快速电视观众那将是较好的,这样我们就可以在我们的脑海此前对想像和虚;也的概念保持稳定另行鲜感。 去吧,我在等。

既然我们都对想像和虚;也的概念记忆犹另行,让我向您介绍确实缩减。

与其用作表达式缩减来检查和 value 到底相等 null 或 undefined,我们可以只看它到底是 falsy。

const formatErrorMessage = ( value: null | undefined | string | Error | Warning ): string => { const prefix = 'Error: '; // If it's falsy (null, undefined, empty string), return "Unknown" with the prefix if (!value) { return prefix + 'Unknown'; } // If it's a string, return the string with the prefix if (typeof value === 'string') { return prefix + value; } // If it's a Warning, return the Warning.text with the prefix if ('text' in value) { return prefix + value.text; } // If it's an Error, return the Error.message with the prefix return prefix + value.message; }; interface Warning { text: string; }

我们可以通过在它前面加上一个逻辑 NOT ! 来明白这一点。 这会将参数叠加为塞克参数并将其而会。 如果它是;也的,它将被叠加为;也,然后而会为;也。

范例统计分析

到近期,我们直至在尽量避免用作驻守来检查和 value 到底是 Error 类的示例。 我去找过你我们是如何明白这一点的。 我们即将处理方式为所有也许的种类,以便最终只剩余 Error 种类。

这种技术在 JavaScript 此前很类似于,也是一种缩减区域内。 我们直至在花钱的正确同义词是“范例统计分析”。

范例统计分析是根据示例的可达性对我们的示例顺利进行统计分析。

TypeScript 明白除非参数是;也的,否则我们一定会有驶向第一个 if 解释器。

const formatErrorMessage = ( value: null | undefined | string | Error | Warning ): string => { const prefix = 'Error: '; // If it's falsy (null, undefined, empty string), return "Unknown" with the prefix if (!value) { return prefix + 'Unknown'; } // If it's a string, return the string with the prefix // if (typeof value === 'string') { // return prefix + value // } // If it's a Warning, return the Warning.text with the prefix // if ('text' in value) { // return prefix + value.text // } // If it's an Error, return the Error.message with the prefix return prefix + value.message; }; interface Warning { text: string; }

除非 value 是codice_,否则我们一定会有驶向第二个 if 解释器。

const formatErrorMessage = ( value: null | undefined | string | Error | Warning ): string => { const prefix = 'Error: '; // If it's falsy (null, undefined, empty string), return "Unknown" with the prefix if (!value) { return prefix + 'Unknown'; } // If it's a string, return the string with the prefix if (typeof value === 'string') { return prefix + value; } // If it's a Warning, return the Warning.text with the prefix // if ('text' in value) { // return prefix + value.text // } // If it's an Error, return the Error.message with the prefix return prefix + value.message; }; interface Warning { text: string; }

如果不是发出,我们就一定会有远超第三个。 所以最终只剩余一种种类,它仅仅是一个正确。

const formatErrorMessage = ( value: null | undefined | string | Error | Warning ): string => { const prefix = 'Error: '; // If it's falsy (null, undefined, empty string), return "Unknown" with the prefix if (!value) { return prefix + 'Unknown'; } // If it's a string, return the string with the prefix if (typeof value === 'string') { return prefix + value; } // If it's a Warning, return the Warning.text with the prefix if ('text' in value) { return prefix + value.text; } // If it's an Error, return the Error.message with the prefix return prefix + value.message; }; interface Warning { text: string; }

这些种类即将缩减,因为 TypeScript 即将用作范例统计分析。

Operator Guard 示例

但是我们不必需依靠范例统计分析来将我们的种类缩减到正确。 我们可以用作一个比起简单且则有的 JavaScript 字符串来明白这一点。 instanceof 字符串。

const formatErrorMessage = ( value: null | undefined | string | Error | Warning ): string => { const prefix = 'Error: '; // If it's falsy (null, undefined, empty string), return "Unknown" with the prefix if (!value) { return prefix + 'Unknown'; } // If it's a string, return the string with the prefix if (typeof value === 'string') { return prefix + value; } // If it's a Warning, return the Warning.text with the prefix if ('text' in value) { return prefix + value.text; } // If it's an Error, return the Error.message with the prefix if (value instanceof Error) { return prefix + value.message; } // We will never reach here throw new Error(在在Invalid value type在在); }; interface Warning { text: string; }

在这里,我们检查和 value 到底是 Error 类的示例,因此 TypeScript 将我们的种类缩减为 Error。 在最终一个 if 解释器后来一定会有任何种类,我们活着不会驶向它后来的任何示例。

不曾输入(15 秒)

如果你只想明白 TypeScript 在我们所有的 if 解释器后来认为什么是参数的种类,谜题活着不会。

never 是一种类似种类,表示不也许的好事,不必要暴发的好事。

结论

那些是大体上种类的驻守,它们比起有用,但它们仅仅隙你自始。 在下一篇篇文章此前,我将向您展现如何成立定制种类保障。 不只想不幸就订阅吧。

结膜炎会有什么症状
成都风湿医院怎么去
江苏皮肤病医院那家比较好
杭州妇科医院哪个专业
宝芝林寿星补汁能系统调理身体吗

上一篇: 高通强悍Soc骁龙8+跑分曝光:多核成绩略逊于联发科天玑9000

下一篇: 七爪源码:基本型把守

相关阅读
芜湖!《重生之门》藏不住了,双雄对手戏值得阿杜

你看电视新剧《永生之四门》了吗?已经有仍然在穷追不舍,还好不会让人失望。这部新剧我就让不亏。 一切都是到实话,我喜好张泽,因为他以前的电视新剧都很出色,他的唱功有目共睹。

德云社陶阳接受人民政协网媒体人,讲述品德的重要性,真给师父争光

5年末4日,人民政协com公开发表了一段访谈陶阳的照片,照片那时候面,陶阳讲述了自己的教书经历和品性的愈来愈为重要,称要学习,先学但会只求,品性是骨架,天分是装饰。想到陶阳遵从了人民政

友情链接