这时候的读者一定知道要写一个 function 来处理这段错误信息: 的确错误信息没有了,但很奇怪的是 gender 竟然变成 never type 了,而这个就是 Type Guard 会做到的一个类型保护机制叫:Narrowing 类型收窄(Narrowing) Narrowing 翻成白话文就是类型收窄,在 TypeScript 的世界中每一个 enum 基本上都是独立存在彼此之间是没有交集的,关系图就像下面这样: image.png
ES.46: Avoid lossy (narrowing, truncating) arithmetic conversions ES.46:避免有损(窄化,截短)算数转换 Reason(原因) A narrowing conversion destroys information, often unexpectedly so. void f(int x, long y, double d) { char c1 = x; // bad: narrowing char c2 = y; // bad: narrowing Enforcement(实施建议) A good analyzer can detect all narrowing conversions. However, flagging all narrowing conversions will lead to a lot of false positives.
The T{e} construction syntax doesn't allow narrowing. Example(示例) For built-in types, the construction notation protects against narrowing and reinterpretation lng) { int x1 = int{ch}; // OK, but redundant int x2 = int{d}; // error: double->int narrowing int; use a reinterpret_cast if you really need to int x4 = int{lng}; // error: long long->int narrowing >int; use a reinterpret_cast if you really need to int y4 = int(lng); // bad: long long->int narrowing
evaluates to -2147467263, which cannot be narrowed to type 'DWORD' (aka 'unsigned int') [-Wc++11-narrowing evaluates to -2147467262, which cannot be narrowed to type 'DWORD' (aka 'unsigned int') [-Wc++11-narrowing evaluates to -2147467260, which cannot be narrowed to type 'DWORD' (aka 'unsigned int') [-Wc++11-narrowing evaluates to -2147467259, which cannot be narrowed to type 'DWORD' (aka 'unsigned int') [-Wc++11-narrowing evaluates to -2147287039, which cannot be narrowed to type 'DWORD' (aka 'unsigned int') [-Wc++11-narrowing
在现代前端开发中,TypeScript 已成为大型项目的标配。然而,仅仅掌握基础类型注解远远不够。类型缩小(Type Narrowing)作为 TypeScript 类型系统的高级特性,是解决实际开发中复杂类型问题的关键技术。
persons.forEach(logPerson); // In case you are stuck: // https://www.typescriptlang.org/docs/handbook/2/narrowing.html #the-in-operator-narrowing 输出Error index.ts(58,16): error TS2339: Property 'role' does not exist on persons.forEach(logPerson); // In case you are stuck: // https://www.typescriptlang.org/docs/handbook/2/narrowing.html #the-in-operator-narrowing 解析: 这里主要是用到了 in 的语法,注意细节如果是 if ("role" in person && person.role) { } 会导致
49.944466640031955 2、源码查看 /** * Returns the value of this {@code Double} as an {@code int} * after a narrowing * @jls 5.1.3 Narrowing Primitive Conversions * * @return the {@code double} value represented
如果一个变量有多种类型,读取该变量时,往往需要进行“类型缩小”(type narrowing),区分该值到底属于哪一种类型,然后再进一步处理。 实际上,联合类型本身可以看成是一种“类型放大”(type widening),处理时就需要“类型缩小”(type narrowing)。 下面是“类型缩小”的另一个例子。
Use = only when you are sure that there can be no narrowing conversions. int> v4{1, 2}; // vector of 2 element with the values 1 and 2 Note(注意) {}-initializers do not allow narrowing variable). {}初始化器不允许窄化转换(这通常是好事)并且允许显式构造函数(这没有问题,我们就是要初始化一个新变量) Example(示例) int x {7.9}; // error: narrowing Enforcement(实施建议) Flag uses of = to initialize arithmetic types where narrowing occurs.
* When used at the type level, all method-level mappings inherit * this primary mapping, narrowing * When used at the type level, all method-level mappings inherit * this primary mapping, narrowing AliasFor("value") String[] path() default {}; /** * The HTTP request methods to map to, narrowing */ RequestMethod[] method() default {}; /** * The parameters of the mapped request, narrowing String[] headers() default {}; /** * The consumable media types of the mapped request, narrowing
isUser).forEach(logPerson); // In case you are stuck: // https://www.typescriptlang.org/docs/handbook/2/narrowing.html isUser).forEach(logPerson); // In case you are stuck: // https://www.typescriptlang.org/docs/handbook/2/narrowing.html
基本原理 Helm的三个重要概念:candidate, narrowing, action. Candidate Candidate即候选值,是一个列表,保存所有可供选择的条目。 Narrowing Helm命令启动后,用户未输入任何关键字前,会将candidate中的所有条目显示出来,每行显示一个项,可通过'C-n', 'C-p'上下移动光标选择当前条目。 这就是narrowing。 值得一提的是,这个过程是动态的,即每输入一个字符,candidate的条目都会被重新筛选。
Type predicates in TypeScript help you narrowing down your types based on conditionals. toUpperCase(x: unknown) { if(isString(x)) { x.toUpperCase(); // ✅ all good, x is string } } 复制代码 Narrowing
基本原理 Helm的三个重要概念:candidate, narrowing, action. Candidate Candidate即候选值,是一个列表,保存所有可供选择的条目。 Narrowing Helm命令启动后,用户未输入任何关键字前,会将candidate中的所有条目显示出来,每行显示一个项,可通过'C-n', 'C-p'上下移动光标选择当前条目。 这就是narrowing。 值得一提的是,这个过程是动态的,即每输入一个字符,candidate的条目都会被重新筛选。
Linux xrdp session Workaround- Linux RDP 会话中无法打开VSCode 解决办法 This time around I resolved the issue by narrowing
收窄请求范围 The * HTTP request methods to map to, narrowing the primary mapping: GET, POST, * HEAD / String[]params() default {}; /** * 映射请求头部,收窄请求范围 The headers of the mapped request, narrowing 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回 The producible media types * of the mapped request, narrowing * When used at the type level, all method-level mappings inherit * this primary mapping, narrowing * When used at the type level, all method-level mappings inherit * this primary mapping, narrowing
Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing
interpret(raw & mask_of<30>); } void demo_misuse() { // [[maybe_unused]] auto x = mask_of<-1>; // narrowing (gcc), or // narrowing+shift (clang) (clang compile error) // [[maybe_unused]] auto y = mask_of <33>; // narrowing, compile error // [[maybe_unused]] auto z = // mask_of<0xFF>; // narrowing (gcc), or narrowing+shift (clang) compile // error } 看懂了吗,还是concept 开源项目需要人手 • asteria[20] 一个脚本语言
收窄请求范围 The * HTTP request methods to map to, narrowing the primary mapping: GET, POST, * HEAD RequestMethod[] method() default {}; /** * 映射请求的参数,收窄请求范围 The parameters of the mapped request, narrowing / String[]params() default {}; /** * 映射请求头部,收窄请求范围 The headers of the mapped request, narrowing 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回 The producible media types * of the mapped request, narrowing * When used at the type level, all method-level mappings inherit * this primary mapping, narrowing
* When used at the type level, all method-level mappings inherit * this primary mapping, narrowing 在源代码中,还有: /** * The HTTP request methods to map to, narrowing the primary mapping: * GET, POST, HEAD