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

Multi tool use


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.
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.
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