ExcelScript.WorksheetProtection interface , instead of
// Pause sheet protection with a password if needed
if (ws.getProtection().getProtected()) {
ws.getProtection().unprotect(shtPW);
}
use (see the link for the entire code):
const protection: ExcelScript.WorksheetProtection = sheet.getProtection();
// Check if the provided password works.
if (protection.checkPassword(password)) {
protection.pauseProtection(password);