javascript - NDEFReader in TypeScript - Stack Overflow

I'm trying to use NDEFReader() for NFC scanwrite in React.This feature works from Chrome 81 (you

I'm trying to use NDEFReader() for NFC scan/write in React. This feature works from Chrome 81 (you can try it on your mobile in Chrome beta today on links below).

GoogleChromeNfcSample, WhatWebCanDoTodayNfc

To enable this feature, you need to go into chrome://flags/ and enable Experimental Web Platform features.

The problem is that I can't make this work in React. I use create-react-app with TypeScript and console output:

Cannot find name 'NDEFReader'

I think that this causes a webpack check. I already tried change some settings in tsconfig.json but nothing worked. Does anyone know, how to enable experimental js/ts pilation, to enable this feature?

I'm trying to use NDEFReader() for NFC scan/write in React. This feature works from Chrome 81 (you can try it on your mobile in Chrome beta today on links below).

GoogleChromeNfcSample, WhatWebCanDoTodayNfc

To enable this feature, you need to go into chrome://flags/ and enable Experimental Web Platform features.

The problem is that I can't make this work in React. I use create-react-app with TypeScript and console output:

Cannot find name 'NDEFReader'

I think that this causes a webpack check. I already tried change some settings in tsconfig.json but nothing worked. Does anyone know, how to enable experimental js/ts pilation, to enable this feature?

Share Improve this question edited Dec 16, 2020 at 13:55 Jeff Posnick 56.2k14 gold badges149 silver badges174 bronze badges asked Mar 9, 2020 at 8:26 Dalibor HomolaDalibor Homola 512 silver badges7 bronze badges 1
  • Found working solution in React --typescript: github./takefumi-yoshii/webnfc-media-memo Demo: webnfc-media-memolify./text-reader – Dalibor Homola Commented Mar 12, 2020 at 9:01
Add a ment  | 

2 Answers 2

Reset to default 7

Web NFC folks provide TypeScript definitions at https://github./w3c/web-nfc/blob/gh-pages/web-nfc.d.ts

// Type definitions for Web NFC
// Project: https://github./w3c/web-nfc
// Definitions by: Takefumi Yoshii <https://github./takefumi-yoshii>
// TypeScript Version: 3.9

// This type definitions referenced to WebIDL.
// https://w3c.github.io/web-nfc/#actual-idl-index

interface Window {
  NDEFMessage: NDEFMessage
}
declare class NDEFMessage {
  constructor(messageInit: NDEFMessageInit)
  records: ReadonlyArray<NDEFRecord>
}
declare interface NDEFMessageInit {
  records: NDEFRecordInit[]
}

declare type NDEFRecordDataSource = string | BufferSource | NDEFMessageInit

interface Window {
  NDEFRecord: NDEFRecord
}
declare class NDEFRecord {
  constructor(recordInit: NDEFRecordInit)
  readonly recordType: string
  readonly mediaType?: string
  readonly id?: string
  readonly data?: DataView
  readonly encoding?: string
  readonly lang?: string
  toRecords?: () => NDEFRecord[]
}
declare interface NDEFRecordInit {
  recordType: string
  mediaType?: string
  id?: string
  encoding?: string
  lang?: string
  data?: NDEFRecordDataSource
}

declare type NDEFMessageSource = string | BufferSource | NDEFMessageInit

interface Window {
  NDEFReader: NDEFReader
}
declare class NDEFReader extends EventTarget {
  constructor()
  onreading: (this: this, event: NDEFReadingEvent) => any
  onreadingerror: (this: this, error: Event) => any
  scan: (options?: NDEFScanOptions) => Promise<void>
  write: (
    message: NDEFMessageSource,
    options?: NDEFWriteOptions
  ) => Promise<void>
}

interface Window {
  NDEFReadingEvent: NDEFReadingEvent
}
declare class NDEFReadingEvent extends Event {
  constructor(type: string, readingEventInitDict: NDEFReadingEventInit)
  serialNumber: string
  message: NDEFMessage
}
interface NDEFReadingEventInit extends EventInit {
  serialNumber?: string
  message: NDEFMessageInit
}

interface NDEFWriteOptions {
  overwrite?: boolean
  signal?: AbortSignal
}
interface NDEFScanOptions {
  signal: AbortSignal
}

This isn't about Webpack checks or your tsconfig, or "experimental JavaScript".

It's just that there are no types for NDEFReader() available, so TypeScript thinks you have a typo.

You can stub in a type for NDEFReader with a file like extra-globals.d.ts (the name doesn't matter so long as it's a .d.ts) in your source tree. This basically tells TypeScript that the global Window interface has one extra field, NDEFReader, whose type you don't really know:

declare global {
  interface Window {
    NDEFReader: any;
  }
}

export {};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744143398a4560306.html

相关推荐

  • javascript - NDEFReader in TypeScript - Stack Overflow

    I'm trying to use NDEFReader() for NFC scanwrite in React.This feature works from Chrome 81 (you

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信