In the S3 client module of AWS, there is a function below, but if you write this way, the error will surely enter the callback, so it should be err?:Error
.
It makes me think that
upload(params:S3.Types.PutObjectRequest,
options?: ManagedUpload.ManagedUploadOptions,
callback?:(err:Error, data:ManagedUpload.SendData)=>void:ManagedUpload;
If this is implemented with if(error){/*error handling*/} else {/*normal handling*/}
, error handling will always be called.
I would appreciate it if someone could solve this question.
typescript optional
Answer from the source code or the link provided, assuming that AWS SDK for JavaScript v2.
This is often the case with API documents that are more often than not.
So let's take a look.
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property
function(err,data){...}
Error
)—an error or null if no error occurred.Therefore, the actual behavior is that the err
parameter contains null
if no errors occur.
In other words, the type annotation is incorrect.
I'm sad to hear that…, so let's try it out.
AWS costs a lot of money, so I'll try Docker localizing the S3-compatible object storage, MINIO, and access it with AWS SDK v2.
docker run -- rm-d-p 9000: 9000 minio/minio server/data
npm install aws-sdk
var AWS=require("aws-sdk");
vars3 = new AWS.S3({
credentials —new AWS.Credentials ("minioadmin", "minioadmin"),
endpoint: new AWS.Endpoint("http://127.0.0.1:9000"),
});
vart=s3.listBuckets(err,data)=>{
console.log({err,data});
});
Thus, you can see that listBuckets
is also a non-null notation for callback, but the actual operation contains null
parameters.
...and the reason why this is happening is, as you can see from the GitHub repository of JavaScript API v2, the original source is JavaScript, not TypeScript.
Therefore, the type information is based on the information written in the JSDoc comment.
Type annotations in JavaScript's type definition files (as well as TypeScript) are used to complement IDE code and Lint, but have no effect on code execution.
Therefore, no matter how many err
parameters are declared nnull not allowed 「 in the type annotation, if null is included at runtime, it is evaluated as null.
In other words, implementing if(err){/*error handling*/}else{/*normal handling*/}
will make the normal process more accurate.
Originally, JavaScript is not a world that writes null secure code, so JSDoc defaults to non-null checking behavior.
For this reason, some *.d.ts generated by JSDoc often return null or undefined even if they are not null-acceptable.
If AWS SDK for JavaScript v3 redesigned with TypeScript, strict is specified at compile time (tsconfig.es.json,tsconfig.cjs.json). (It is also explicitly written as a new feature of v3)
Therefore, you should be able to write a null secure code using v3.
© 2024 OneMinuteCode. All rights reserved.