manuelBetancurt
manuelBetancurt

Reputation: 16158

implementing JS function into typescript

I'm implementing a JS function into typescript, it works fine on JS, but on TS, I get:

[ts] Cannot find name 'PasscodeAuth'. Did you mean 'passcodeAuth'?

function passcodeAuth() {
  return PasscodeAuth.isSupported()
    .then(() => {
      return PasscodeAuth.authenticate()
    })
    .then(success => {
      AlertIOS.alert('Authenticated Successfully');
    })
    .catch(error => {
      console.log(error)
      AlertIOS.alert(error.message);
    });
}

So is not an import, or defined anywhere, but it works on JS, so how can I make it recognizable for TS? On JS, I can name my parameter as any word on JS and it will have isSupported and authenticate, even though is now a real parameter?

thanks!

Upvotes: 3

Views: 58

Answers (1)

CRice
CRice

Reputation: 32176

Putting aside the issue of if PasscodeAuth actually exists, you can inform typescript of it by using a declare statement. For your example:

// Put this at the root level of the file that uses PasscodeAuth
declare const PasscodeAuth: any;

That will declare PasscodeAuth as a variable with type any. You can then use it in your file without errors related to that variable, but since it's typed as an any, you won't be getting any type safety from it. If you happen to know the type for it, you can specify a more specific type instead of any.

A declare statement is only a type annotation, so that line won't actually have any effect on the emitted javascript code. You mentioned that your code already works as-is, despite the typescript errors, so a declaration sounds like exactly what you need.

Upvotes: 3

Related Questions