Is it possible convert from *.js to *.ts by simply ading type definitions in the same file?

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Is it possible convert from *.js to *.ts by simply ading type definitions in the same file?



Now, I have a code:


const log = (m) => {
console.log(m);
return m;
};



and want to convert to a valid *.ts code by simply adding a type definition line without touching the existing JavaScript lines,


declare type log = (m: unknown) => unknown;
const log = (m) => { //Error: [ts] Parameter 'm'
console.log(m); //implicitly has an 'any' type.
return m; // (parameter) m: any
};



and got an Error:


[ts] Parameter 'm' implicitly has an 'any' type.
(parameter) m: any



Is this inevitable? Is there any workaround?



Please consider providing a concise response,



a) Yes, there's a workaround, and the code is...



b) No, there's no way, sure, it's inevitable because of ...REASON...



c) I'm not sure the answer is a) or b), I don't know there's a workaround or not. So, I just want to comment.



Obviously, c) is not much appreciated.



and I probably can't give any response to "Why do you want to do that" type Questions.



Thanks.





Your approach misses the point of having types: the types are defined to be able to declare the variables as using them. In order to turn your JS files into valid TS files you have to define the types and declare the type of each variable. Unused, the types are just garbage in the file.
– axiac
19 mins ago





Btw, type log and const log define two unrelated entities named log: a type and a variable.
– axiac
18 mins ago




type log


const log


log





@axiac Oh sure, I forgot to "declare" the type, and modified the first line, and got the same result. Which turns out it is still garbage, and please read my Question mentioning to be concise if there is a workaround or not, or I have just add c) I don't know there's a workaround or not, but just want to comment.
– bayesian-study
8 mins ago





@axiac If your answer is b) No, there's no way, sure, it's inevitable because of ...REASON..., why don't you answer as so. I will mark your answer as accepted, and this question is over, correct? Thanks.
– bayesian-study
5 mins ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

6f cUux9lt2nkHySYOzTRSHoKzI1Cl5RAKgz,yz,T9qCYiF8WfuSvbcy4bCly ANCOtImkY,MliiV1B8udhHOCwgNx
ryaiuat7HnIIOrTIOyhh3q,b8 gUE nOTfYhflAqa80V,RlJwb F3ZVty Zh5P SW1tIxMSxxBXIJ23

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham