typescript - ขยายการกำหนดค่า .d.ts ของ DefinitelyTyped
ฉันพยายามใช้ nightmare ใน typescript กับประเภทจาก DefinitelyTyped อย่างไรก็ตาม ประเภทที่นั่นไม่ครบถ้วน (เมทอด then หายไป) และฉันก็อยากจะเพิ่มเมทอดบางอย่างแบบไดนามิกด้วย (เหมือนที่ทำในปลั๊กอินนี้)
ฉันไม่เข้าใจว่าจะขยายอินเทอร์เฟซของ Nightmare โดยไม่ต้องแยกโฟลเดอร์ DefinitelyTyped ได้อย่างไร ฉันได้อ่านบทความไม่กี่หน้าเกี่ยวกับคำถามเหล่านี้:
ขยายอินเทอร์เฟซที่กำหนดในไฟล์ .d.ts
http://obaidurrehman.github.io/2015/10/30/extending-typeScript-definitions/
ดังนั้นฉันจึงพยายามสร้างไฟล์ extendNightmare.d.ts ที่ฉันวางไว้พร้อมกับไฟล์ .ts อื่นทั้งหมดของฉัน
declare namespace Nightmare {
class Nightmare {
waitForDevTools(): void;
then(): Nightmare;
}
}
ในจุดนี้ ฉันไม่รู้ว่าจะทำอย่างไรเพื่อบอกคอมไพเลอร์ให้ใช้คำนิยามใหม่ของฉันที่ควรขยายจากคำนิยามที่มีอยู่แล้ว
ฉันพยายามที่จะเพิ่มบรรทัดนี้ที่ด้านบนของไฟล์โดยใช้ nightmare แต่ก็ไม่ได้ผล:
/// <reference path="./extendNightmare.d.ts"/>
นอกจากนี้ฉันอยากขยายอินเทอร์เฟซ IConstructorOptions จากเนมสเปซ Nightmare แต่ฉันไม่รู้ว่าจะทำอย่างไร นี่ถูกต้องหรือไม่?
declare namespace Nightmare {
class Nightmare {
waitForDevTools(): void;
then(): Nightmare;
}
export interface IConstructorOptions {
switches: any;
}
}
โค้ดที่ฉันใช้ในการทำซ้ำปัญหานี้:
/// <reference path="./extendNightmare.d.ts"/>
import * as Nightmare from "nightmare";
const switches = {
"ignore-certificate-errors": true,
"proxy-server": "localhost:10000",
};
function render_page(url) {
const options = {
switches,
};
const nightmare = new Nightmare(options);
nightmare
.goto(url)
.then((infos) => {
console.info(infos);
})
.end()
.then(() => {
console.info(`${url}: done`);
});
}
render_page("https://google.com");
ใน package.json ของฉัน ฉันมี:
"nightmare": "^2.9.1",
"@types/nightmare": "^1.6.30",