问题
想把条件判断提取成独立方法,同时让 TypeScript 正确理解类型。原始写法中,isTargetListenFile 返回的是 boolean,收窄信息丢失了:
this.editorChangeEventRef = this.plugin.app.workspace.on(
"editor-change",
(editor, info) => {
const file = info.file;
if (!this.isTargetListenFile(file)) return;
let currDebounceUpdateFuc = this.filePathToDebouncedUpdateFucMap.get(file.path);
if (!currDebounceUpdateFuc) {
currDebounceUpdateFuc = debounce(() => {
this.handleEditorChange(file);
}, this.dubounceIntervalMs);
this.filePathToDebouncedUpdateFucMap.set(file.path, currDebounceUpdateFuc);
}
currDebounceUpdateFuc();
}
);
private isTargetListenFile(file: TFile | TAbstractFile | null): boolean {
if (file instanceof TFile && file.extension === "md") return true;
return false;
}
解决:使用类型谓词
把返回类型从 boolean 改成类型谓词 file is TFile,TypeScript 就能在 if 之后把 file 收窄为 TFile:
this.editorChangeEventRef = this.plugin.app.workspace.on(
"editor-change",
(editor, info) => {
const file = info.file;
if (!this.isTargetListenFile(file)) return;
let currDebounceUpdateFuc = this.filePathToDebouncedUpdateFucMap.get(file.path);
if (!currDebounceUpdateFuc) {
currDebounceUpdateFuc = debounce(() => {
this.handleEditorChange(file); // file 类型正确
}, this.debounceIntervalMs);
this.filePathToDebouncedUpdateFucMap.set(file.path, currDebounceUpdateFuc);
}
currDebounceUpdateFuc();
}
);
private isTargetListenFile(file: TFile | TAbstractFile | null): file is TFile {
return file instanceof TFile && file.extension === "md";
}
类型谓词的工作原理
file is TFile 告诉 TypeScript:
- 函数返回
true时,file的类型是TFile - 返回
false时,类型不变,仍是TFile | TAbstractFile | null
更精细的类型定义
如果想要更严格的类型,可以定义一个类型别名:
type TargetFile = TFile & { extension: "md" };
private isTargetListenFile(file: TFile | TAbstractFile | null): file is TargetFile {
return file instanceof TFile && file.extension === "md";
}
如果需要额外检查
检查逻辑更复杂时,可以把各部分拆开:
private setupEditorChangeListener() {
this.editorChangeEventRef = this.plugin.app.workspace.on(
"editor-change",
this.handleEditorChange.bind(this)
);
}
private handleEditorChange = (editor: Editor, info: EditorChangeInfo) => {
const file = info.file;
if (!this.isTargetListenFile(file)) return;
if (this.isFileExcluded(file)) return;
this.debouncedFileUpdate(file);
};
private isTargetListenFile(file: TFile | TAbstractFile | null): file is TFile {
return file instanceof TFile && file.extension === "md";
}
private isFileExcluded(file: TFile): boolean {
return this.excludePaths.some(path => file.path.startsWith(path));
}
private debouncedFileUpdate = (() => {
const debouncedHandlers = new Map<string, () => void>();
return (file: TFile) => {
let handler = debouncedHandlers.get(file.path);
if (!handler) {
handler = debounce(() => {
this.handleEditorChange(file);
}, this.debounceIntervalMs);
debouncedHandlers.set(file.path, handler);
}
handler();
};
})();
类型谓词 vs 类型断言
// 类型谓词(推荐):运行时检查 + 类型安全
private isTargetListenFile(file: TFile | TAbstractFile | null): file is TFile {
return file instanceof TFile && file.extension === "md";
}
// 类型断言:假设成立,可能出错
if (!(file as TFile)?.path) return;
小结
用 parameter is Type 处理这类重构最合适:既保持类型安全,又支持逻辑分离,还能在多个地方复用同一份类型检查逻辑。谓词函数应当保持纯粹,只做类型检查;逻辑复杂时可考虑缓存结果。
顺带修正了原代码中的拼写错误 this.dubounceIntervalMs,正确写法是 this.debounceIntervalMs。