diff --git a/docs/modules/index.ts.md b/docs/modules/index.ts.md index 8f9717b..407d09b 100644 --- a/docs/modules/index.ts.md +++ b/docs/modules/index.ts.md @@ -831,12 +831,8 @@ export function printStatic(node: Node, i: number = 0): string { ... } **Signature** ```ts -export function property( - key: string, - type: TypeReference, - isOptional: boolean = false, - description?: string -): Property { ... } +export function property(key: string, type: TypeReference, description?: string): Property +export function property(key: string, type: TypeReference, isOptional?: boolean, description?: string): Property { ... } ``` # readonlyArrayCombinator (function) diff --git a/src/index.ts b/src/index.ts index 0ebb22a..c6369a8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -301,12 +301,23 @@ export function identifier(name: string): Identifier { } } +export function property(key: string, type: TypeReference, description?: string): Property +export function property(key: string, type: TypeReference, isOptional?: boolean, description?: string): Property export function property( key: string, type: TypeReference, - isOptional: boolean = false, + isOptional: boolean | string = false, description?: string ): Property { + if (typeof isOptional === 'string') { + return { + kind: 'Property', + key, + type, + isOptional: false, + description: isOptional // actually description from the overloaded signature + } + } return { kind: 'Property', key, diff --git a/test/property.ts b/test/property.ts new file mode 100644 index 0000000..c2ab562 --- /dev/null +++ b/test/property.ts @@ -0,0 +1,18 @@ +import * as assert from 'assert' +import * as t from '../src' + +describe('property overload', () => { + it('should be the same without description', () => { + const longForm = t.typeCombinator([t.property('foo', t.stringType, false)]) + const shortForm = t.typeCombinator([t.property('foo', t.stringType)]) + + assert.deepEqual(shortForm, longForm) + }) + + it('should be the same with description', () => { + const longForm = t.typeCombinator([t.property('foo', t.stringType, false, 'test')]) + const shortForm = t.typeCombinator([t.property('foo', t.stringType, 'test')]) + + assert.deepEqual(shortForm, longForm) + }) +})