Forge Viewer的DefinitelyTyped声明缺失Profiles等相关类型如何补充?
解决方案
- 本地补充临时类型声明
在项目中新建typings/forge-viewer.d.ts文件,写入以下内容扩展现有类型定义,可根据自己实测的API参数调整字段类型:
import type { Viewer3D } from 'forge-viewer'; declare global { namespace Autodesk.Viewing { interface ProfileSettings { // 内置配置枚举值,可根据实际运行返回值调整类型 AEC: string; clone(settings: Partial<ProfileSettings>): ProfileSettings; } class Profile { constructor(customProfileSettings: Partial<ProfileSettings>); } interface Viewer3D { registerProfile(profile: Profile, profileId: string): void; setProfile(profile: string | Profile): void; impl: { // 三个参数依次为最低FPS、目标FPS、最高FPS setFPSTargets(minFps: number, targetFps: number, maxFps: number): void; } } } }
之后在tsconfig.json的include字段中添加该类型文件的路径,确保TS可以识别到扩展的类型。
- 类型校准方法
如果遇到参数不匹配的情况,可以直接在浏览器控制台运行对应的API,打印出入参要求和返回值结构,调整上面的类型定义即可:
- 查看ProfileSettings结构:
console.dir(Autodesk.Viewing.ProfileSettings) - 查看setFPSTargets入参要求:
console.log(viewer.impl.setFPSTargets.toString())
- 长期方案
你可以将校验完成的类型定义提交到DefinitelyTyped的forge-viewer类型包,供所有开发者共用。
内容的提问来源于stack exchange,提问作者Vi100
相关产品推荐
相关产品推荐

