w3c / web-nfc

Web NFC
https://w3c.github.io/web-nfc/
Other
311 stars 68 forks source link

Add type NDEFRecordType to web-nfc.d.ts #629

Closed drewmw5 closed 2 years ago

drewmw5 commented 2 years ago

This pull request is for adding types to recordTypes.

beaufortfrancois commented 2 years ago

As you can see at https://w3c.github.io/web-nfc/#data-mapping, recordType can be any string that follows a certain pattern (See local type and external type).

Is it possible in TypeScript to have something like below?

  readonly recordType: NDEFRecordTypes | string;

or

type NDEFRecordTypes = string | "absolute-url" | "empty" | "mime" | "smart-poster" | "text" | "unknown" | "URL";
drewmw5 commented 2 years ago

As you can see at https://w3c.github.io/web-nfc/#data-mapping, recordType can be any string that follows a certain pattern (See local type and external type).

Is it possible in TypeScript to have something like below?

  readonly recordType: NDEFRecordTypes | string;

or

type NDEFRecordTypes = string | "absolute-url" | "empty" | "mime" | "smart-poster" | "text" | "unknown" | "URL";

Ah, good to know, there's still plenty I have left to read. Thanks for that. You are correct, the following example you gave is valid TypeScript, the other one was not:

type NDEFRecordTypes = string | "absolute-url" | "empty" | "mime" | "smart-poster" | "text" | "unknown" | "URL";

Is there more to it than that?

beaufortfrancois commented 2 years ago

@takefumi-yoshii Can you have a look as well?

zolkis commented 2 years ago

As @beaufortfrancois mentioned earlier, record type can be a string. How would you write this example after this PR? https://w3c.github.io/web-nfc/#write-an-external-record-with-an-ndef-message-as-payload

Then, smart poster local types should also be listed?

zolkis commented 2 years ago

Well, to me it seems that if we cannot add to the definitions a generic "string" as a fallback, then we should leave recordType as string. Static type validation as "string" should be enough, and dynamic validation, including standard record types defined by the NFC Forum, is defined by the algorithms in the spec, and it will depend on context (where the records belong). If there are shortcomings there, please file issues.