ละเลยข้อผิดพลาดของ TypeScript ที่เกิดจากแพ็กเกจบุคคลที่สามที่นำเข้า
ฉันต้องการนำเข้าไฟล์ TypeScript ที่อยู่ในแพ็กเกจ (node_modules) ไฟล์ภายนอกนี้มีข้อผิดพลาดเกี่ยวกับชนิด เมื่อฉันรันคอมไพเลอร์ TypeScript โดยไม่ได้นำเข้าไฟล์นี้ จะไม่มีรายงานข้อผิดพลาดใดๆ อย่างไรก็ตาม ฉันต้องการไฟล์นี้ทั้งเนื้อหาและการประกาศชนิด ทุกครั้งที่ฉันนำเข้าไฟล์ ข้อผิดพลาดที่อยู่ในไฟล์ก็จะถูกรายงานเมื่อฉันรันคอมไพเลอร์
ผมได้พยายามที่จะ include เฉพาะโฟลเดอร์ src ของผม, exclude ไฟล์ภายนอก, ทำให้ compilerOptions.types เป็นอาร์เรย์ว่าง, ตั้งค่า compilerOptions.skipLibCheck เป็น true และ/หรือใส่ //@ts-ignore ในตำแหน่งที่ผม import แล้ว แต่ไม่มีวิธีไหนช่วยได้เลย
พิจารณาว่าไฟล์นั้นอยู่ใน node_modules และฉันไม่มีสิทธิ์เข้าถึงไลบรารีนั้น ฉันจึงไม่สามารถเข้าไปในไฟล์และแก้ไขข้อผิดพลาดหรือเพียงแค่เขียน //@ts-ignore ในไฟล์นั้นได้ เพราะการเปลี่ยนแปลงทั้งหมดจะถูกเขียนทับในอนาคต
ตัวอย่างเช่น,
//@ts-ignore
import IntersectSrc from 'vuetify/src/directives/intersect'
export function foo() {
console.log("Hello World")
IntersectSrc.inserted(document.createElement('div'), {})
}
export type InsertFunctionType = typeof IntersectSrc['inserted']
ด้วย tsconfig.json ดังต่อไปนี้
{
"compilerOptions": {
"target": "ES2016",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules",
"node_modules/vuetify/src/directives/intersect/index.ts"
]
}
รายงานข้อผิดพลาด Property '_observe' ไม่มีอยู่ในประเภท 'HTMLElement' จาก node_modules/vuetify/src/directives/intersect/index.ts.
ดังนั้นคำถามของฉันคือ มีวิธีใดบ้างที่จะละเว้นข้อผิดพลาดที่เกิดจากการนำเข้าชุดแพ็กเกจจากภายนอก?