javascripttypescriptwebauthnpasskey

Why navigator.credentials.create return Credential instead of PublicKeyCredential in typescript?


I am using typescript 5.6 ("typescript": "~5.6")

When I trying to use WebAuth, I found that navigator.credentials.create return Promise<Credential | null>

enter image description here enter image description here

But in MDN, I think it should be PublicKeyCredential (Although it inherits from Credential)


Solution

  • navigator.credentials.create is quite flexible and lets you create multiple types of credentials, not just PublicKeyCredential, but also among other FederatedCredential and PasswordCredential. This all depends on the type of options you send in with your request.

    As you say in the question, PublicKeyCredential inherits from Credential, so a PublicKeyCredential is always a Credential, even if a Credential might not always be an PublicKeyCredential. But if you are sending in a PublicKeyCredentialCreationOption to navigator.credentials.create you can expect the Credential you get returned to be a PublicKeyCredential, or NULL.

    See documentation on create() method for more info