FrankKai
FrankKai
### 项目中xxx.d.ts的declare module '*.scss'是什么意思?`declare module`还可以做什么? #### 项目中xxx.d.ts的declare module '*.scss'是什么意思? ```js // externals.d.ts declare module '*.scss' ``` > 默认情况下import style from 'style.scss'在ts的ide校验器里会报错,那就用d.ts假定定义所有scss结尾的文件是module。--社长 假设将declare module '*.scss'注释掉,ide会报错,但是可以通过lint。 #### `declare module`还可以做什么? 当我们引入了一个微软官方@types/*中不存在的自定义包时,ide会报错。 例如下面这样: ...
### typescript如何约束Promise的类型? Promise泛型函数 ```js interface Promise { then(onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null): Promise;...
### typescript中的keyof如何使用? #### 最简 ```js type Point = { x: number; y: number }; type P = keyof Point; // 'x' | 'y' let foo: P = 'x'; let bar:...
### typescript中的typeof如何使用? js中的typeof主要用于表达式上下文,而ts中的typeof主要用于类型上下文。 ```js let s = "hello"; let n: typeof s; // ^ = let n: string ``` ```js type Predicate = (x: unknown) => boolean; type K =...
### typescript中的`non-null assert operator`是什么? 非null断言操作符:当为null时,发生断言,抛出异常。 可选链:当为null/undefined时,返回undefined。 #### 非空断言操作符和可选链操作符测试 ```js // Non-Null Assertion Operator const obj = null; interface Entity { name?: string; } // 非空断言操作符 function nonNull(e?: Entity) { const...
### enum比object可以更加方便地由value获取key(仅限数字枚举,字符串枚举不支持) 传统的obj由value获取到key比较麻烦: ```js const obj = { foo: 1, bar: 9, baz: 100 } // 查找value为9的key for(const [key, value] of Object.entries(obj)){ if(value === 9) return key } // 或者...
### 元素隐式具有 "any" 类型,因为类型为 "any" 的表达式不能用于索引类型 修改tsconfig.json中的一个配置: ```json { "compilerOptions": { "suppressImplicitAnyIndexErrors": true, }, } ```
### 数组['zh', 'en']与泛型的SomeType有什么区别? 数组['zh', 'en']是js对象,是value。需要通过typeof转化为类型。 泛型内容的['zh', 'en']是类型,是type。 看个"将元组转为联合类型的类型体操"例子对比一下: 例如: ```js const LANGS = ['zh', 'en'] as const; type T_TRANS = T[number]; // 传入数组 type T_LANGS = T_TRANS; // 等价于 type...
### 常用yarn命令小结 - 安装全局包 `yarn global add @vue/cli` - 查看全局安装包 ``` yarn global bin cd usr/local/bin ls ``` - 删除全局包 ``` yarn global remove @vue/cli ``` - 升级单个包 ``` yarn...
### yarn使用经验 #### yarn更新包但package.json不同步更新 yarn upgrade axios@^0.18.1 (指定版本更新就行了,仅仅yarn upgrade axios不行) #### Package.json中锁了某个npm包的版本,npm包升级以后,重新yarn,会升级到最新版本吗? 要看是否有lock,有的话不会,会锁定;没有的话,会升级。 #### 如果在lock的情况下去升级包,如何去做? `yarn upgrade [package]@[version] `或者 删除yarn.lock,重新yarn install