-
-
Notifications
You must be signed in to change notification settings - Fork 12
Description
I believe TypL could benefit from implementing documentation functionality.
Types are useful for documentation but they're only a partial strategy. If a developer were to use TypL for a project and also wanted documentation they'd likely reach for JSDoc and omit type information. JSDoc is widespread but it's an old, inactive project with clunky, generally disliked syntax (I only have anecdotal evidence for this claim).
I propose TypL could enter this field to 1) give developers a modern documentation system and 2) enhance TypL itself with more detailed runtime error descriptions. This would both bring more attention to TypL itself and increase the value of and use cases for TypL.
Nice IDE integration for JSDoc-like tooltips would be a big bonus too.
Usage example with rough API:
function getCommaSeparatedNumberStringPair (aBool = bool, aNumber = number, aString = string) {
return aBool ? string`${aNumber}, ${aString}` : string`No bueno.`
}
documentation (getCommaSeparatedNumberStringPair, {
description: 'This function joins a number and string together to create a new string.',
parameters: {
aBool: 'some description of aBool',
aNumber: 'some description of aNumber',
aString: 'some description of aString',
},
returns: 'aNumber comma separated from aString if aBool is true, otherwise a default message.',
})
getCommaSeparatedNumberStringPair(anArbitraryBool, anArbitraryString)Resulting runtime error:
TypL Error:
Invalid types for getCommaSeparatedNumberStringPair.
Received Arguments:
1. VALID: a boolean value
4. INVALID: a string value
5. INVALID: no value given
Function Parameters:
1. aBool: boolean - some description of aBool
2. aNumber: number - some description of aNumber
3. aString: string - some description of aString
Suggestion:
Fix Received Argument types for #2 and #3 to match Function Parameters.
"Function Parameters" above is where we see the mixture of TypL's type information and the user-generated documentation. I'm sure we could come up with other benefits of mixing type information and documentation as well.
documentation could be configurable to spit out more or less information. For example: a developer could configure the output to include description and returns, unlike what you see above.
Benefits of a function-based API (as opposed to JSDoc style comments):
- Modularity
- Composability
- Flexibility
- Ergonomic
- Smaller learning curve, no special syntax
- Automatic IDE support for syntax
Basically, it's JS, do what you want to.
Simple example showing how an application could have a common dictionary of terms to be used around the codebase
// commonDictionary.docs.js
export const organizationId = string`the id for an organization`
export const organizations = string`a list of organizations`
// myModule.js
import { organizationId, organizations } from './commonDictionary.docs.js'
const deleteOrganization = (organizationId = string, organizations = array) => {
return organizations.filter (organization => organization.id !== organizationId)
}
documentation (deleteOrganization, {
description: 'This function removes an organization',
parameters: {
organizationId,
organizations,
},
returns: 'the updated organizations',
})
const updateOrganization = (organizationId = string, organizations = array, updateProperties = object) => {
return organizations.map (organization => {
return organization.id === organizationId
? { organization, ...updateProperties }
: organization
})
}
documentation (updateOrganization, {
description: 'This function updates an organization',
parameters: {
organizationId,
organizations,
updateProperties: 'the properties to update an organization with',
},
returns: 'the updated organizations',
})