Add DOM Serial API types (#47514)

This commit is contained in:
Maciej Mroziński 2020-09-14 23:54:01 +02:00 committed by GitHub
parent fab36eb1e4
commit 88283446e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 115 additions and 0 deletions

View 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
View 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;
}

View 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"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }