我的前端库的代码被拆分成几个源文件。
例子:
// a.ts
function a() {}
// b.ts
function b() {}
// main.ts
const myLib = {
a: a,
b: b
}
Run Code Online (Sandbox Code Playgroud)
我需要构建一个仅myLib导出 的ES6 模块(即一个 JavaScript 文件)作为默认导出。
我看到两个选项。第一个:
tsc将每个文件编译为 JavaScript;my-lib.js;export …)。第二个:
my-lib.ts;export default myLib;tsc在连接的文件上运行。这两个选项都很丑陋并且map文件丢失。
有没有更好的方法来做到这一点?
我知道类型脚本中的类型联合和类型交集,但我找不到使用类型排除的语法或变通方法.有没有办法做到这一点?
type ValidIndices = string ^ '_reservedProperty'; // All strings but '_reservedProperty'
interface MyInterface {
[property: ValidIndices]: number;
_reservedProperty: any;
}
Run Code Online (Sandbox Code Playgroud) 我正在浏览打字稿的文档并找到了一个例子
interface ClockConstructor {
new (hour: number, minute: number): ClockInterface;
}
interface ClockInterface {
tick();
}
function createClock(ctor: ClockConstructor, hour: number, minute: number): ClockInterface {
return new ctor(hour, minute);
}
class DigitalClock implements ClockInterface {
constructor(h: number, m: number) { }
tick() {
console.log("beep beep");
}
}
class AnalogClock implements ClockInterface {
constructor(h: number, m: number) { }
tick() {
console.log("tick tock");
}
}
let digital = createClock(DigitalClock, 12, 17);
let analog = createClock(AnalogClock, 7, 32);
Run Code Online (Sandbox Code Playgroud)
我得到了接口的所有概念,但我坚持在匿名对象声明的接口中有一个新关键字的概念
interface …Run Code Online (Sandbox Code Playgroud) 我想确认当then同一个promise上有多个回调时,是否保证传递给的回调的调用顺序.
这是我观察到的.例:
function wait(delayMs) {
return new Promise(resolve => setTimeout(resolve, delayMs))
}
let prom = wait(500)
for (let i = 0; i < 20; ++i)
prom.then(() => { console.log(i) }) // OK: Display 0 to 19 in the right order
Run Code Online (Sandbox Code Playgroud)
我观察到回调顺序是受到尊重的,但我没有找到关于这个主题的任何文档.回调订单是否有保证?
编辑:这不是一个如何链接承诺的问题.在这里,我只有一个承诺有几个回调.回调then以确定的顺序传递.我想知道是否也确定了回调执行的顺序.