我正在尝试实现Web OTP API Typescript类型问题中描述的解决方案-Typescript中缺少类型,但我得到以下错误:
Property 'code' does not exist on type 'CredentialType'.
有人能帮我理解并解决这个问题吗?
示例:
async otpRequest() {
if ('OTPCredential' in window) {
const abortController = new AbortController();
let timer = setTimeout(() => {
abortController.abort();
}, 10 * 1000);
let o: CredentialRequestOptions = {
otp: { transport: ['sms'] },
signal: abortController.signal
};
const content = await navigator['credentials'].get(o);
// here: Property 'code' does not exist on type 'CredentialType'
console.log(content.code)
}).catch(err => {
console.log(err);
});
}
}
似乎类型CredentialType只包含PasswordCredential|FederatedCredential| PublicKeyCredential
由于来自的规范https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/webappsec-credential-management/index.d.ts
并且它缺少OTPCredential,很可能不完整,需要从确定性类型中跟进,所以我通过添加来临时解决问题
interface CredentialType {
code: string
type: string
}
进入polyfill.dts.
问题解决了。