Add typings for form-serialize (#27403)

* add form-serialize

* minor changes to more accurately describe form-serialize defaults
This commit is contained in:
Tyler Johnson 2018-07-23 12:37:05 -06:00 committed by Andy
parent 1ba5f19d13
commit f109e22895
4 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,17 @@
import serialize = require("form-serialize");
const form = document.createElement("form");
serialize(form, { hash: true }); // $ExpectType ResultHash
serialize(form, true); // $ExpectType ResultHash
serialize(form, { hash: false }); // $ExpectType string
serialize(form, false); // $ExpectType string
serialize(form); // $ExpectType string
interface CustomResult {
foo: string;
}
// $ExpectType CustomResult
serialize(form, {
serializer: (): CustomResult => ({ foo: "bar" })
});

37
types/form-serialize/index.d.ts vendored Normal file
View File

@ -0,0 +1,37 @@
// Type definitions for form-serialize 0.7
// Project: https://github.com/defunctzombie/form-serialize#readme
// Definitions by: Tyler Johnson <https://github.com/tyler-johnson>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
interface ResultHash {
[key: string]: string | string[] | ResultHash;
}
interface Options<Result> {
/** Configure the output type. If true, the output will be a js object. */
hash?: boolean;
/**
* Optional serializer function to override the default one. The function takes 3 arguments (result, key,
* value) and should return new result hash and url encoded str serializers are provided with this module
*/
serializer?: (result: Result, key: string, value: string) => Result;
/** If true serialize disabled fields. */
disabled?: boolean;
/** If true serialize empty fields */
empty?: boolean;
}
interface OptionsHash extends Options<ResultHash> {
hash: true;
}
interface OptionsString extends Options<string> {
hash: false;
}
declare function serialize(form: HTMLFormElement, options: OptionsHash | true): ResultHash;
declare function serialize(form: HTMLFormElement, options?: OptionsString | false): string;
declare function serialize<Result = string>(form: HTMLFormElement, options?: Options<Result> | boolean): Result;
export = serialize;

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"form-serialize-tests.ts"
]
}

View File

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