-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FP S4123 (no-invalid-await): TypeScript Pick
type
#2658
Comments
I looked into this today. The critical lines are in no-invalid-await.ts: function hasThenMethod(type: ts.Type) {
const thenProperty = type.getProperty('then');
return Boolean(thenProperty && thenProperty.flags & ts.SymbolFlags.Method);
} For the code in the bug (using Ok, easy peasy, add that as a valid case: function hasThenMethod(type: ts.Type) {
const thenProperty = type.getProperty('then');
return Boolean(
thenProperty &&
(thenProperty.flags & ts.SymbolFlags.Method || thenProperty.flags & ts.SymbolFlags.Transient),
);
} Unfortunately, that leads to failure on this test case: async function foo() {
await {then: 42};
} Which I found quite surprising, that TypeScript could not differentiate between those two cases. They truly have exactly identical flags of 33554436. With all of that said, in my opinion, a false positive is worse than a false negative. |
We're seeing similar behavior with the new import { stat, mkdir } from 'fs/promises';
/**
*
* @param {URL} dirUrl
*/
export const ensureDirExists = async (dirUrl) => {
let exists = true;
try {
await stat(dirUrl);
} catch (err) {
exists = false;
}
if (!exists) {
await mkdir(dirUrl, { recursive: true });
}
}; |
I'm having this issue just inside standard JS without imports. Here's my testcase: const getLoginStatus = async () => {
return true;
};
const startApp = async () => {
if (await getLoginStatus() === true){ // Refactor this redundant 'await' on a non-promise.
console.log('go to dashboard route');
}
else {
console.log('go to login route');
}
};
startApp(); However, this seems to work just fine, no linting errors appearing: const startApp = async () => {
if (await Promise.resolve(true) === true){
console.log('go to dashboard route');
}
else {
console.log('go to login route');
}
};
startApp(); |
Pick
type
Creating new issue as requested by @vilchik-elena in #2636
The following TypeScript code snippet creates a “Refactor this redundant ‘await’ on a non-promise.” warning, which is incorrect.
I see the error both in the SonarLint VS Code plugin (v2.0.0) and the current version of SonarCloud.
The
Pick
type is a Typescript "utility" type. It creates a dynamic interface. For examplePick<Todo, "title" | "completed">
would be an interface with only the "title" and "completed" fields in the "Todo" interface.The
keyof
operator returns all of the keys/members of a type.As far as I can tell, these should be equivalent:
To be candid, I don't use either of these features myself. This is just my understanding.
My project uses Knex, which is a popular SQL builder. Knex uses this pattern extensively, and then creates many false positives.
Here is the root declaration in Knex:
https://github.com/knex/knex/blob/dfbc76b915fff9f40e806ac2e0aafe8e4e5b6965/types/index.d.ts#L1742
Originally posted by @codyebberson in #2636 (comment)
The text was updated successfully, but these errors were encountered: