小编kal*_*lpa的帖子

如何从全局包运行 npm 脚本?

比如说,我正在制作一个 npm 包。

示例 package.json:

{
   "bin": { "cli": "cli.js" },
   "scripts": { 
    "sample": "node sample.js" 
   } 
}
Run Code Online (Sandbox Code Playgroud)

示例 cli.js:

const shell = require('shelljs')
shell.exec('npm run sample')
Run Code Online (Sandbox Code Playgroud)

然后,我跑npm link

现在,如果我cli项目存储库以外的任何地方运行,它都不会运行。相反,它会抛出错误。

我通过将 cli.js 更改为以下方式找到了解决方法:

const shell = require('shelljs')
, package_path = require('./path.json') 
// I manually created this path.json containing the absolute path of the package

shell.exec('npm run --prefix ${package_path} sample')
Run Code Online (Sandbox Code Playgroud)

这种作品。但主要限制是:

  • 项目的所有贡献者必须在克隆存储库后手动设置此路径。

  • 如果像这样全局安装软件包,npm i -g package那么这种路径更改会让用户感到烦恼。

我要问的是:

  • 如何自动设置路径?

  • 还有其他更好的方法来实现相同的行为,即npm script从全局 …

node.js npm

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

“在分配变量之前使用变量”(TS2454)的模式没有帮助

考虑以下代码模式,

class stuff {
    public id: string;
    public uid: number;
    constructor(parameter: string){
        this.id = parameter;
    }
    public getUID(): number {
        return Date.now();
    }
}

let ids: number[]; /* 1: Here variable is not assigned */
for ( let i = 0; i < 100; i++){
    ids[i] = new stuff(i.toString()).getUID();
    /* 2: Here ids[i] is used before initialized */
}
Run Code Online (Sandbox Code Playgroud)

在这种模式下,typescript 将无法使用"strict": true.

我可以这样做let ids:number[] | undefined,但这失去了使用打字稿的价值。

有没有其他模式可以实现相同的行为?

谢谢。

typescript

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

'Set'仅指类型,但在此处用作值.(TS2693)

请考虑以下代码以检查数组是否有重复项.

let arr: number[] = [1,2,3,1];

function hasDuplicates (arr: number[]): boolean {
  return new Set(arr).size !== arr.length;
}
Run Code Online (Sandbox Code Playgroud)

但在这里我遇到了typescript编译错误

'Set' only refers to a type, but is being used as a value here. (TS2693)
Run Code Online (Sandbox Code Playgroud)

有什么建议 ?

typescript

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

如何在Mocha内部测试使用setTimeout()的函数?

考虑以下功能,

function helloAfter100ms(){
  setTimeout(function(){
    console.log('hello');
  },100)
}
Run Code Online (Sandbox Code Playgroud)

用摩卡测试代码,

describe('#helloAfter100ms()',function(){
  it('console logs hello ONLY after 100ms',function(){
    // what should go here
  })
})
Run Code Online (Sandbox Code Playgroud)

javascript mocha.js

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

如何在同步功能中使用“等待”?

我想制作这样的 API:

class jsonReader {
  public async load()
  {
     // some code
  }
}

let reader = new jsonReader();

function foo(){
  await reader.load();
  // [ts] 'await' expression is only allowed within an async function.
}
Run Code Online (Sandbox Code Playgroud)

如何在同步函数调用中使用 Async/Await?

typescript

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

如何将类型转换为 undefined 或 null?

我想强制一个对象超出范围,例如,

let obj: typeA = aTypeAobj;

function del(obj: typeA): void {
  obj = undefined;
}
Run Code Online (Sandbox Code Playgroud)

但是strict模式下的打字稿不允许我这样做。

我可以这样做,

let obj: typeA = aTypeAobj;

function del(obj: typeA | undefined): void {
  obj = undefined;
}
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,我也可以传递任何undefined类型。

有没有办法在函数体内进行类型转换,例如,

let obj: typeA = aTypeAobj;

function del(obj: typeA): void {
  obj<typeA | undefined> = undefined; // this doesn't work but I'm asking something like this
}
Run Code Online (Sandbox Code Playgroud)

然后我可以获得 ts 阻止任何undefined类型作为参数的优势del()以及将typeA对象分配给undefined.

谢谢。

typescript

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

标签 统计

typescript ×4

javascript ×1

mocha.js ×1

node.js ×1

npm ×1