One can add type hints for a plain old javascript object by defining a class for it, and adding a comment above the constructor defining the parameter types
onEdit(e) (when a cell is edited) has an event, e as an input.
Unfortunately, it there is no Event type in the script editor.
We can fix that by defining an event class and adding a type hint for the parameter e like so:
class User {
/**
* @param {string} email
* @param {string} nickname
*/
constructor(email, nickname) {
this.email = email;
this.nickname = nickname;
}
}
class Range {
/**
* @param {Number} columnEnd
* @param {Number} columnStart
* @param {Number} rowEnd
* @param {Number} rowStart
*/
constructor(columnEnd, columnStart, rowEnd, rowStart) {
this.columnEnd = columnEnd;
this.columnStart = columnStart;
this.rowEnd = rowEnd;
this.rowStart = rowStart;
}
}
class Event {
/**
* @param {string} value
* @param {User} user
* @param {ScriptApp.AuthMode} authMode
* @param {Range} range
* @param {string} oldValue
*/
constructor(value, user, authMode, range) {
this.value = value;
this.user = user;
this.authMode = authMode;
this.range = range;
}
}
/**
* @param {Event} e
*/
function onEdit(e) {
// now we have autocompletion on e properties
SpreadsheetApp.getUi().alert(JSON.stringify(e));
SpreadsheetApp.getUi().alert(e.user.email);
}
name
parameter really an instance of aString
and not a string literal? When annotating with JSDoc, please, pay attention to what each type means,string
andString
do not mean the same thing. – UleRange
type is – UleRange
is, because Google itself manages it? Surely if I matched what they write in their JSDoc for e.g.SpreadsheetApp
methods, I could get the same behaviour? – Lineate@types/google-apps-script
npm package - your life will never be the same again. That said, Google plans to migrate to a new online editor that uses the same engine (Monaco), so when they release it, any effort poured into trying to work around current limitations will go to waste. – Ule