GVKun编程网logo

[TypeScript] Define Custom Type Guard Functions in TypeScript

20

最近很多小伙伴都在问[TypeScript]DefineCustomTypeGuardFunctionsinTypeScript这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展13

最近很多小伙伴都在问[TypeScript] Define Custom Type Guard Functions in TypeScript这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展13、TypeScript 之联合类型 - unionType、null、undefined、angular – 由typescript导入的代码的Typescript类型?、electron 7 typescript @types/node v13 问题、javascript – 为什么从angularjs.TypeScript.DefinitelyTyped中删除了全局“angular”变量?等相关知识,下面开始了哦!

本文目录一览:

[TypeScript] Define Custom Type Guard Functions in TypeScript

[TypeScript] Define Custom Type Guard Functions in TypeScript

One aspect of control flow based type analysis is that the TypeScript compiler narrows the type of a variable within a type guard.

This lesson explores how you can define functions and type predicates to create your own type guards similar to the Array.isArray() method.

 

const numbers = [0, 1, 2, [3, 4], 5, [6], [7], 8, [9]];

function isFlat<T>(array: (T | T[])[]): array is T[] {
    console.log(!array.some(Array.isArray));
return !array.some(Array.isArray); }
if (isFlat(numbers)) { numbers; }

isFlat function return value is a boolean value. We add ''array is T[]'' that adds additional information for types.

 

isFlat(numbers): numbers type is ''(number|number())[]'' 

but inside if statement: numbers is ''number[]'', because we tell typescript, array is T[] in the return value.

13、TypeScript 之联合类型 - unionType、null、undefined

13、TypeScript 之联合类型 - unionType、null、undefined

所谓联合类型就是指:可以指定多种数据类型,扩大数据类型范围。

let data: string | number | boolean...

TS并不推荐使用

null 和 undefined 是所有类型的子集 有些情况 即使你定义参数类型 传入 null 或者 undefined 也不会报错的

当你 去编译 TS 文件 加上后缀 --strictNullChecks

比如这样tsc filesName.ts --strictNullChecks

var func = function (value: number | string) {
 var type = typeof value;
 if (typeof value === "number") {
  return "your number is " + value;
 }
 else if (typeof value === "string") {
  return "your name is " + value;
 }
};
var result = func(null);

clipboard.png

意思为类型“null”的参数不能赋值给类型 “string | number” 的参数

当然想解决此类问题 只需要在参数加上 null 和 undefined 即可

angular – 由typescript导入的代码的Typescript类型?

angular – 由typescript导入的代码的Typescript类型?

这是一个Angular应用程序的问题,但我不认为这是一个角度特定的问题.

我正在开发一个Angular 7应用程序,我正在使用一些库,这些库是通过前端的脚本标签从其他来源(Stripe,recaptcha,googletags等)导入的.我将在这里使用Stripe作为示例,因为Stripe绝对要求客户端从Stripe.js导入前端库以实现PCI兼容性,因此从源服务器端不是一个选项.

我已经从DefinitelyTyped安装了Stripe的类型.如果我添加声明让Stripe:any;并且在组件中使用Stripe,它可以工作,但当然它不是类型安全的.如果我省略声明,VSCode发现类型很好,编辑器捕获类型错误,但它不会编译,(错误TS2304:找不到名称’Stripe’.)可能因为我没有Stripe的import语句.

有没有办法提示TypeScript它应该使用DefinitelyTyped中的类型来捕获类型错误,而不导入Stripe库本身?

编辑:我收回我所说的关于不认为这是Angular特定的内容.我现在认为这可能与角度编译过程有关.

解决方法

我过去遇到了同样的问题,我也发现在不导入实际模块的情况下使用导入导入类型定义文件是不可能的.

所以,我所做的是,我将Type DeFinition文件(通常是index.d.ts)复制到我的代码库中.

之后,您可以执行以下操作:

import {SomeType} from "sometype.d.ts"
declare let ObjectImportedFromHTMLScriptTag: SomeType;

然后,您可以使用导入的对象而不会破坏类型安全性.

electron 7 typescript @types/node v13 问题

electron 7 typescript @types/node v13 问题

如果运行v13 版本的node types 会发现有如下错误

错误信息

node_modules/electron/electron.d.ts:7145:33 - error TS2689: Cannot extend an interface ''NodeJS.EventEmitter''. Did you mean ''implements''?

原因

当前electron 7 不支持@types/node 13 版本的

解决方法

使用12 版本的比较好

  • 命令
yarn add @types/node@12.12.6 --dev

参考

https://github.com/electron/electron/issues/21612
https://electronjs.org/blog/electron-7-0

总结

以上是小编为你收集整理的electron 7 typescript @types/node v13 问题全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

原文地址:https://www.cnblogs.com/rongfengliang/p/12209518.html

javascript – 为什么从angularjs.TypeScript.DefinitelyTyped中删除了全局“angular”变量?

javascript – 为什么从angularjs.TypeScript.DefinitelyTyped中删除了全局“angular”变量?

从 angularjs.TypeScript.DefinitelyTyped 2.1.6升级到3.1.2删除了我用于模块声明文件的静态角度变量的可访问性.

以下现在会出现无法正确解析符号角度的错误.

angular
    .module("someModule")....

Resharper提供了从外部模块导入变量的选项,但这不起作用.

早期版本中全局角度变量的曝光是错误吗?是不打算以我的项目使用它的方式使用它?

解决方法

这是Resharper 9.0的一个错误.该修复程序计划在9.1版本中发布.您现在可以执行以下两项操作之一来解决问题:

>转到工具 – >选项 – > Resharper->停用Resharper暂停.请注意,您将丢失所有Resharper功能,并且您可能希望重新启用VS的Intellisense.
>如果您对预发布版本感到满意,请获取最新的9.1 EAP release.

关于[TypeScript] Define Custom Type Guard Functions in TypeScript的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于13、TypeScript 之联合类型 - unionType、null、undefined、angular – 由typescript导入的代码的Typescript类型?、electron 7 typescript @types/node v13 问题、javascript – 为什么从angularjs.TypeScript.DefinitelyTyped中删除了全局“angular”变量?的相关知识,请在本站寻找。

本文标签: