Product: SonarCloud
Plugin: javascript
Version: 8.9 (build 17411)
Rule Key: typescript:S3001
When delete operator is used on optional properties of object, an issue is raised (details):
It looks like a compliant code though.
Consider this package.json
:
{
"name": "sample",
"version": "1.0.0",
"main": "main.ts",
"dependencies": {
"aws-sdk": "^2.1058.0",
"proxy-agent": "^5.0.0",
"typescript": "^4.5.4",
"@types/node": "^17.0.9"
}
}
And this main.ts
:
import { config } from 'aws-sdk';
import proxy from 'proxy-agent';
console.log('--- Config default---');
console.log(config.httpOptions);
console.log('deletion: ' + delete config.httpOptions?.agent);
console.log('--- Config default after deletion ---');
console.log(config.httpOptions);
config.update({
httpOptions: { agent: proxy('http://proxy.company.com') },
});
console.log('--- Config after update ---');
console.log(config.httpOptions);
console.log('deletion: ' + delete config.httpOptions?.agent);
console.log('--- Config after deletion ---');
console.log(config.httpOptions);
Output after tsc --esModuleInterop true main.ts && node main.js
:
--- Config default---
{ timeout: 120000 }
deletion: true
--- Config default after deletion ---
{ timeout: 120000 }
--- Config after update ---
{
timeout: 120000,
agent: ProxyAgent {
proxy: Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'proxy.company.com',
port: null,
hostname: 'proxy.company.com',
hash: null,
search: null,
query: null,
pathname: '/',
path: '/',
href: 'http://proxy.company.com/'
},
proxyUri: 'http://proxy.company.com',
proxyFn: [Function: httpOrHttpsProxy]
}
}
deletion: true
--- Config after deletion ---
{ timeout: 120000 }
The deletion is done as expected.