V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
ericgui

TypeScript compiler 是不是对 typeof 进行了重新定义?

  •  
  •   ericgui · May 7, 2021 · 1828 views
    This topic created in 1816 days ago, the information mentioned may be changed or developed.

    const a = { x: 1 }

    typeof a // 'object'

    但在 TypeScript 里,typeof a 就是 : { x: number }

    所以我觉得是不是 ts 的 compiler 对 typeof 做了一些加工,尤其是非基本类型的场景下,变得更加强大了,而不是仅仅输出一个 object 就算完了

    3 replies    2021-05-07 10:30:53 +08:00
    mxT52CRuqR6o5
        2
    mxT52CRuqR6o5  
       May 7, 2021 via Android
    准确的的说并不是重新定义,是在类型表达式中给与新的含义,而在原来的 js 代码中仍然是原来的 typeof
    noe132
        3
    noe132  
       May 7, 2021 via Android   ❤️ 1
    let t = typeof a // 这里的 typeof 是运算符,这个是 runtime 执行的

    type T = typeof a // 这里的 typeof 是 ts 的类型运算符,是在 compile time 执行的

    这 2 个名字一样,但不是同一个东西
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5771 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 07:17 · PVG 15:17 · LAX 00:17 · JFK 03:17
    ♥ Do have faith in what you're doing.