mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
Add DOM Serial API types (#47514)
This commit is contained in:
parent
fab36eb1e4
commit
88283446e1
32
types/dom-serial/dom-serial-tests.ts
Normal file
32
types/dom-serial/dom-serial-tests.ts
Normal file
@ -0,0 +1,32 @@
|
||||
navigator.serial.addEventListener('connect', event => {
|
||||
console.log(event.target);
|
||||
});
|
||||
|
||||
navigator.serial.addEventListener('disconnect', event => {
|
||||
console.log(event.target);
|
||||
});
|
||||
|
||||
async function connect() {
|
||||
const port = await navigator.serial.requestPort({
|
||||
filters: [
|
||||
{
|
||||
usbVendorId: 0x2341,
|
||||
},
|
||||
],
|
||||
});
|
||||
await port.open({
|
||||
baudrate: 9600,
|
||||
parity: 'none',
|
||||
buffersize: 128,
|
||||
});
|
||||
const info = port.getInfo();
|
||||
const vendor = info.vendor;
|
||||
port.writable.getWriter();
|
||||
port.readable.getReader();
|
||||
}
|
||||
|
||||
navigator.serial.requestPort().then(port => {
|
||||
navigator.serial.getPorts().then(ports => {
|
||||
ports.length;
|
||||
});
|
||||
});
|
||||
58
types/dom-serial/index.d.ts
vendored
Normal file
58
types/dom-serial/index.d.ts
vendored
Normal file
@ -0,0 +1,58 @@
|
||||
// Type definitions for non-npm package Web Serial API based on spec and Chromium implementation 0.1
|
||||
// Project: https://wicg.github.io/serial/
|
||||
// Definitions by: Maciej Mroziński <https://github.com/maciejmrozinski>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
type EventHandler = (event: Event) => void;
|
||||
|
||||
interface SerialPortInfoBase {
|
||||
serialNumber: string;
|
||||
manufacturer: string;
|
||||
locationId: string;
|
||||
vendorId: string;
|
||||
vendor: string;
|
||||
productId: string;
|
||||
product: string;
|
||||
}
|
||||
|
||||
interface SerialPortFilter {
|
||||
usbVendorId: number;
|
||||
usbProductId?: number;
|
||||
}
|
||||
|
||||
interface SerialPortInfo extends SerialPortInfoBase, SerialPortFilter {} // mix spec and Chromium implementation
|
||||
|
||||
type ParityType = 'none' | 'even' | 'odd' | 'mark' | 'space';
|
||||
|
||||
type FlowControlType = 'none' | 'hardware';
|
||||
|
||||
interface SerialOptions {
|
||||
baudrate: number; // Chromium implementation (spec: baudRate)
|
||||
dataBits?: number;
|
||||
stopBits?: number;
|
||||
parity?: ParityType;
|
||||
buffersize?: number; // Chromium implementation (spec: bufferSize)
|
||||
flowControl?: FlowControlType;
|
||||
}
|
||||
|
||||
interface SerialPort {
|
||||
open(options: SerialOptions): Promise<void>;
|
||||
readonly readable: ReadableStream; // Chromium implementation (spec: in)
|
||||
readonly writable: WritableStream; // Chromium implementation (spec: out)
|
||||
getInfo(): Partial<SerialPortInfo>;
|
||||
}
|
||||
|
||||
interface SerialPortRequestOptions {
|
||||
filters: SerialPortFilter[];
|
||||
}
|
||||
|
||||
interface Serial extends EventTarget {
|
||||
onconnect: EventHandler;
|
||||
ondisconnect: EventHandler;
|
||||
getPorts(): Promise<SerialPort[]>;
|
||||
requestPort(options?: SerialPortRequestOptions): Promise<SerialPort>; // Chromium implementation (spec: SerialOptions)
|
||||
}
|
||||
|
||||
interface Navigator {
|
||||
readonly serial: Serial;
|
||||
}
|
||||
24
types/dom-serial/tsconfig.json
Normal file
24
types/dom-serial/tsconfig.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6",
|
||||
"dom"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictFunctionTypes": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"dom-serial-tests.ts"
|
||||
]
|
||||
}
|
||||
1
types/dom-serial/tslint.json
Normal file
1
types/dom-serial/tslint.json
Normal file
@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Loading…
Reference in New Issue
Block a user