小编Los*_*lds的帖子

运算符'=='不能应用于Typescript 2中的类型x和y

TypeScript版本: 2.0.2.0

代码 我知道代码有点愚蠢,但我实际上在我的代码中进行了这些测试(制作表达式访问者),我真的认为这些应该立即飞行和编译.

var a: boolean = (true == false);
var b: boolean = (5 == 2);
Run Code Online (Sandbox Code Playgroud)

相反,它抱怨操作数相等不能应用于类型'true','false','5'和'2'.标记它们不是布尔值或数字,它们实际上是'true','false','5','2'的类型.我知道类型'string'和'boolean'无法比较,但是嘿,5实际上是一个数字,不是'5'类型,还是我弄错了?

这编译虽然.

let x = 2;
var a: boolean = 5 == x;
var b: boolean = <number>5 == <number>2;
Run Code Online (Sandbox Code Playgroud)

我错过了什么,为什么不将5和2视为类型'数字'?

预期的行为: 应该编译

实际行为: 导致编译错误,导致'操作数'=='无法应用于类型'<first argument>'和'<second argument>'

背景我在打字稿中遇到了这个问题,定义它应该是这样的,但是为什么会这样? https://github.com/Microsoft/TypeScript/issues/6167

type-conversion typescript typescript2.0

29
推荐指数
3
解决办法
4万
查看次数

在我要求之前如何不执行承诺

我以为我已经把这一切都想到了Promise,但这个实际上让我起床了.在使用带有两个参数的执行程序创建一个新的Promise时,为什么这个方法在我接受then()或catch()之前运行

运行节点6.2.2.

import assert = require('assert');

describe("When working with promises", () => {
    let ar = [1, 2, 3, 4, 5, 6];

    beforeEach(() => {

    })

    it("should be perfectly fine but isn't when mapping to promises", (done) => {
        ar.map(num => {
            return new Promise((resolve, reject) => {
                done(new Error('Why on earth is ' + num + ' called'));
            })
        })

        done();
    })

    it("should be perfectly fine when mapping to methods", (done) => {

        ar.map(num => {
            return (resolve, reject) …
Run Code Online (Sandbox Code Playgroud)

node.js es6-promise

1
推荐指数
1
解决办法
781
查看次数