shjames

Results 10 comments of shjames

'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015...

function fn(x: string): void; function fn() { // ... } 上面函数重载的写法中,在函数实现部分,为什么fn()直接这样写,没有带参数也没有报错,是因为假如在函数实现中,如果没有传参,就直接跳过参数部分的类型检查啦?

图片加载失败了

请问大佬,这个泛型写法,什么时候写在前面,什么时候写在后面有口诀吗? ``` //这里是写在后面 class GenericNumber { zeroValue: NumType; add: (x: NumType, y: NumType) => NumType; } ``` ``` //写在前面 interface GenericIdentityFn { (arg: Type): Type; } ```

下面的函数体怎么写才能不报红? ![avatar](http://temp-ma-client.huodonghezi.vip/imagesite/media/image/2022/5/17/wIU0lzpgmc.png) ![avatar](http://temp-ma-client.huodonghezi.vip/imagesite/media/image/2022/5/17/rPDHNfrwRP.png)

@kwSir-chen > > 下面的函数体怎么写才能不报红? ![avatar](https://camo.githubusercontent.com/db054f09776a48776ae16e8b2a913259ee56000e01b5dd33c088f87d3f8f93c0/687474703a2f2f74656d702d6d612d636c69656e742e68756f646f6e6768657a692e7669702f696d616765736974652f6d656469612f696d6167652f323032322f352f31372f774955306c7a70676d632e706e67) ![avatar](https://camo.githubusercontent.com/4bf86933deca6af8fc8d473eb217a68c4b24bf5ebb24ae558d7dfcf996d6513a/687474703a2f2f74656d702d6d612d636c69656e742e68756f646f6e6768657a692e7669702f696d616765736974652f6d656469612f696d6167652f323032322f352f31372f725044484e66727752502e706e67) > > 可能这么写 > ``` > function createLabel( > idOrName: T > ): NameOrId { > if (typeof idOrName === 'number') { > return...

@riskers > > 下面的函数体怎么写才能不报红? ![avatar](https://camo.githubusercontent.com/db054f09776a48776ae16e8b2a913259ee56000e01b5dd33c088f87d3f8f93c0/687474703a2f2f74656d702d6d612d636c69656e742e68756f646f6e6768657a692e7669702f696d616765736974652f6d656469612f696d6167652f323032322f352f31372f774955306c7a70676d632e706e67) ![avatar](https://camo.githubusercontent.com/4bf86933deca6af8fc8d473eb217a68c4b24bf5ebb24ae558d7dfcf996d6513a/687474703a2f2f74656d702d6d612d636c69656e742e68756f646f6e6768657a692e7669702f696d616765736974652f6d656469612f696d6167652f323032322f352f31372f725044484e66727752502e706e67) > > ```typescript > > function createLabel(idOrName: number): IdLabel; > function createLabel(idOrName: string): NameLabel; > function createLabel(idOrName: number | string) : IdLabel | NameLabel...

@riskers > @shjames https://stackoverflow.com/questions/60475431/generic-type-extending-union-is-not-narrowed-by-type-guard > > 可以参考这个问题的回答 > > `idOrName` 是参数,可能是 string 也可能是 number,这是编译期不能决定的,所以没办法通过 `NameOrId` 来约束返回值 > > 好的,感谢

ReadonlyArray类型不能修改指的是不能使用类似数组push的一些方法吗?比如let x: readonly string[] = [];x=['1','2']; 直接这样些并没有报错

![avatar](http://temp-ma-client.huodonghezi.vip/imagesite/media/image/2022/4/28/Ojbouxqybt.png) 请问大佬,为什么上面这种间接赋值会报错?而下面这种直接赋值就不会? ![avatar](http://temp-ma-client.huodonghezi.vip/imagesite/media/image/2022/4/28/1iqw3MpFBv.png)