The Parser For JSDoc Types.
npm install @typedefs/parser
@typedefs/parser is The Parser For _JSDoc_ Types Written Using Google Closure Compiler Annotations (no _TypeScript_ support). Although most of the typing rules are the same, the main difference is for functions and arrays:
- ✅ function(string, number=): void
- ⛔️ (arg: string, optional?: number) => void The arrow function notation is not supported. Cannot write ? for optional arguments, need to use =.
- ✅ !Array
- ⛔️ string[] The double array bracket notation will not work.
- ✅ { record: (string|undefined) }
- ⛔️ { record?: string } Optional properties cannot be denoted with a question mark.
``sh`
yarn add @typedefs/parser
- Table Of Contents
- API
- parse(type: string): !Type
* Type
* FunctionType
- Copyright
The package is available by importing its default function:
`js`
import parse from '@typedefs/parser'
parse(
): !TypeParses a Google Closure Compiler type recursively.
__Type__: The representation of a type.
| Name | Type | Description |
| ----------- | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| nullable | boolean | Whether the type is nullable. This is defined by writing ? before the type name to state nullability and ! otherwise. The parser does not infer nullability from types being primitive and Function/function. |(string\|number)
| name | string | The name of the type. |
| union | !Array<!Type> | If the type is defined as a union, e.g., , contains the united types. Must include parenthesis. |Object
| record | !Object<string, Type> | If the type is a record, contains its representation. If a property of the record does not have a type, it will be set to null. |
| application | !Array<!Type> | The application of the type, e.g., the inner type of . |function(string=)
| function | !FunctionType | The function info with args and return if the type is a function. |
| optional | boolean | If the type is returned as an optional argument of a function (), this will be set to true. |
__FunctionType__: The meta information about the function.
| Name | Type | Description |
| ------------ | ------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| __args*__ | !Array<!Type> | The arguments of the function. |
| __return*__ | ?Type | The return type of the function. When the value is set to null, it means the function does not have a return. If the return was actually null, it would be specified as return: { name: 'null' }. |this
| this | !Type | The type of the argument specified as function(this: Type). |new
| new | !Type | The type of the argument specified as function(new: Type). |function(...Type)
| variableArgs | !Type | The type of the variable arguments, e.g., . |
`js
import parser from '@typedefs/parser'
logHeading('Applications')
log(parser('!Object
logHeading('Unions (parenthesis are required)')
log(parser('(string | number | !Promise(string | symbol)>)'))
logHeading('Records')
log(parser({
a: string, b: ?number, c,
d: !Promise
f: { g: boolean }
}))
logHeading('Functions')
log(parser(function(
this: Type,
string,
function(),
function(): *=
): function(): null))
// special case when name is nullable empty string ''
log(parser(function(): ?))``js
Applications:
------------
{ nullable: false,
name: 'Object',
application:
[ { name: 'string' },
{ name: 'Promise',
application:
[ { name: 'Array',
application: [ { nullable: false, name: 'Type' } ] } ] } ] }
Unions (parenthesis are required):
---------------------------------
{ union:
[ { name: 'string' },
{ name: 'number' },
{ nullable: false,
name: 'Promise',
application:
[ { union: [ { name: 'string' }, { name: 'symbol' } ],
nullable: true } ] } ] }
Records:
-------
{ record:
{ a: { name: 'string' },
b: { nullable: true, name: 'number' },
c: null,
d:
{ nullable: false,
name: 'Promise',
application:
[ { name: 'Array',
application: [ { record: { e: { name: 'number' } } } ] } ] },
f: { record: { g: { name: 'boolean' } } } } }
Functions:
---------
{ name: 'function',
function:
{ return:
{ name: 'function',
function: { return: { name: 'null' }, args: [] } },
args:
[ { name: 'string' },
{ name: 'function', function: { return: null, args: [] } },
{ name: 'function',
function: { return: { name: 'any' }, args: [] },
optional: true } ],
this: { name: 'Type' } } }
{ name: 'function',
function: { return: { nullable: true, name: '' }, args: [] } }
``
alt="Art Deco"> | © Art Deco 2019 | alt="Tech Nation Visa"> | Tech Nation Visa Sucks |
|---|