I'm trying to create a tiny library with the following structure:
library('input').method()
This is the code (the addQuotes method adds quotes to the string input):
const library = function (text: string) {
interface ValueObject {
addQuotes?: Function
} // Without this, I get "Property 'to' does not exist on type '{}'."
const options: ValueObject = {}
options.addQuotes = function () {
if (!text) return ''
return `"${text}"`
}
return options
}
const result = library('test').addQuotes()
console.log(result)
Right now, TypeScript is underlying this bit:
library('test').addQuotes()
Telling me this:
Object is possibly 'undefined'.
What could be the problem and how to fix it?
https://jsbin.com/vonawucada/1/edit?js,console
(The code runs here, but in my VS Code setup, I do get the TypeScript error.)