Typescript types for code that is not imported by typescript?
up vote
4
down vote
favorite
This is an issue with an Angular app, but I don't believe this is an angular-specific issue.
I'm working on an Angular 7 app, and I'm working with a few libraries that are imported via script tags on the front end from other sources (Stripe, recaptcha, googletags, etc). I'll use Stripe as an example here, because Stripe absolutely requires that the front end library be imported from Stripe.js
by the client for PCI compliance, so from source server side is not an option.
I've installed the types for Stripe
from DefinitelyTyped. If I add declare let Stripe: any;
and use Stripe
in a component, it works, but of course it is not type safe. If I omit the declaration, VSCode finds the types fine, and the editor catches type errors, but it won't compile, (error TS2304: Cannot find name 'Stripe'.
) presumable because I have no import
statement for Stripe.
Is there any way to hint to TypeScript that it should use the types from DefinitelyTyped to catch type errors, without importing the Stripe library itself?
EDIT:
OK, I think I've figured out what specifically leads to the problem. This may be expected behavior:
It fails to compile with:
class Foo
constructor()
bar()
console.log(Stripe);
But it works with:
class Foo
constructor()
bar = () =>
console.log(Stripe);
It seems to be related to scope, but I'm not sure I understand exactly why this would be.
Edit again: I take back what I said about not thinking this is Angular specific. I now think this might have something to do with the angular compilation process.
angular typescript angular7
add a comment |
up vote
4
down vote
favorite
This is an issue with an Angular app, but I don't believe this is an angular-specific issue.
I'm working on an Angular 7 app, and I'm working with a few libraries that are imported via script tags on the front end from other sources (Stripe, recaptcha, googletags, etc). I'll use Stripe as an example here, because Stripe absolutely requires that the front end library be imported from Stripe.js
by the client for PCI compliance, so from source server side is not an option.
I've installed the types for Stripe
from DefinitelyTyped. If I add declare let Stripe: any;
and use Stripe
in a component, it works, but of course it is not type safe. If I omit the declaration, VSCode finds the types fine, and the editor catches type errors, but it won't compile, (error TS2304: Cannot find name 'Stripe'.
) presumable because I have no import
statement for Stripe.
Is there any way to hint to TypeScript that it should use the types from DefinitelyTyped to catch type errors, without importing the Stripe library itself?
EDIT:
OK, I think I've figured out what specifically leads to the problem. This may be expected behavior:
It fails to compile with:
class Foo
constructor()
bar()
console.log(Stripe);
But it works with:
class Foo
constructor()
bar = () =>
console.log(Stripe);
It seems to be related to scope, but I'm not sure I understand exactly why this would be.
Edit again: I take back what I said about not thinking this is Angular specific. I now think this might have something to do with the angular compilation process.
angular typescript angular7
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
This is an issue with an Angular app, but I don't believe this is an angular-specific issue.
I'm working on an Angular 7 app, and I'm working with a few libraries that are imported via script tags on the front end from other sources (Stripe, recaptcha, googletags, etc). I'll use Stripe as an example here, because Stripe absolutely requires that the front end library be imported from Stripe.js
by the client for PCI compliance, so from source server side is not an option.
I've installed the types for Stripe
from DefinitelyTyped. If I add declare let Stripe: any;
and use Stripe
in a component, it works, but of course it is not type safe. If I omit the declaration, VSCode finds the types fine, and the editor catches type errors, but it won't compile, (error TS2304: Cannot find name 'Stripe'.
) presumable because I have no import
statement for Stripe.
Is there any way to hint to TypeScript that it should use the types from DefinitelyTyped to catch type errors, without importing the Stripe library itself?
EDIT:
OK, I think I've figured out what specifically leads to the problem. This may be expected behavior:
It fails to compile with:
class Foo
constructor()
bar()
console.log(Stripe);
But it works with:
class Foo
constructor()
bar = () =>
console.log(Stripe);
It seems to be related to scope, but I'm not sure I understand exactly why this would be.
Edit again: I take back what I said about not thinking this is Angular specific. I now think this might have something to do with the angular compilation process.
angular typescript angular7
This is an issue with an Angular app, but I don't believe this is an angular-specific issue.
I'm working on an Angular 7 app, and I'm working with a few libraries that are imported via script tags on the front end from other sources (Stripe, recaptcha, googletags, etc). I'll use Stripe as an example here, because Stripe absolutely requires that the front end library be imported from Stripe.js
by the client for PCI compliance, so from source server side is not an option.
I've installed the types for Stripe
from DefinitelyTyped. If I add declare let Stripe: any;
and use Stripe
in a component, it works, but of course it is not type safe. If I omit the declaration, VSCode finds the types fine, and the editor catches type errors, but it won't compile, (error TS2304: Cannot find name 'Stripe'.
) presumable because I have no import
statement for Stripe.
Is there any way to hint to TypeScript that it should use the types from DefinitelyTyped to catch type errors, without importing the Stripe library itself?
EDIT:
OK, I think I've figured out what specifically leads to the problem. This may be expected behavior:
It fails to compile with:
class Foo
constructor()
bar()
console.log(Stripe);
But it works with:
class Foo
constructor()
bar = () =>
console.log(Stripe);
It seems to be related to scope, but I'm not sure I understand exactly why this would be.
Edit again: I take back what I said about not thinking this is Angular specific. I now think this might have something to do with the angular compilation process.
angular typescript angular7
angular typescript angular7
edited Nov 21 at 5:21
Cœur
17.1k9102140
17.1k9102140
asked Nov 7 at 22:53
Karptonite
7231722
7231722
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I had the same problem in the past, and I also find it impossible to import the Type Definition files using import
without importing the actual module.
So, what I did is, I copied the Type Definition file (usually index.d.ts
) into my codebase.
After that, you could do something like:
import SomeType from "sometype.d.ts"
declare let ObjectImportedFromHTMLScriptTag: SomeType;
Then, you could use the imported object without breaking type safety.
Not a good idea to copy past but how it works if you direct access to this d.ts from node_modules ?
– Gilsdav
Nov 8 at 8:04
@Gilsdav It will work as well without importing the actual module, however please remember to install the typings using--save-dev
options.
– Wong Jia Hau
Nov 8 at 8:19
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I had the same problem in the past, and I also find it impossible to import the Type Definition files using import
without importing the actual module.
So, what I did is, I copied the Type Definition file (usually index.d.ts
) into my codebase.
After that, you could do something like:
import SomeType from "sometype.d.ts"
declare let ObjectImportedFromHTMLScriptTag: SomeType;
Then, you could use the imported object without breaking type safety.
Not a good idea to copy past but how it works if you direct access to this d.ts from node_modules ?
– Gilsdav
Nov 8 at 8:04
@Gilsdav It will work as well without importing the actual module, however please remember to install the typings using--save-dev
options.
– Wong Jia Hau
Nov 8 at 8:19
add a comment |
up vote
0
down vote
I had the same problem in the past, and I also find it impossible to import the Type Definition files using import
without importing the actual module.
So, what I did is, I copied the Type Definition file (usually index.d.ts
) into my codebase.
After that, you could do something like:
import SomeType from "sometype.d.ts"
declare let ObjectImportedFromHTMLScriptTag: SomeType;
Then, you could use the imported object without breaking type safety.
Not a good idea to copy past but how it works if you direct access to this d.ts from node_modules ?
– Gilsdav
Nov 8 at 8:04
@Gilsdav It will work as well without importing the actual module, however please remember to install the typings using--save-dev
options.
– Wong Jia Hau
Nov 8 at 8:19
add a comment |
up vote
0
down vote
up vote
0
down vote
I had the same problem in the past, and I also find it impossible to import the Type Definition files using import
without importing the actual module.
So, what I did is, I copied the Type Definition file (usually index.d.ts
) into my codebase.
After that, you could do something like:
import SomeType from "sometype.d.ts"
declare let ObjectImportedFromHTMLScriptTag: SomeType;
Then, you could use the imported object without breaking type safety.
I had the same problem in the past, and I also find it impossible to import the Type Definition files using import
without importing the actual module.
So, what I did is, I copied the Type Definition file (usually index.d.ts
) into my codebase.
After that, you could do something like:
import SomeType from "sometype.d.ts"
declare let ObjectImportedFromHTMLScriptTag: SomeType;
Then, you could use the imported object without breaking type safety.
answered Nov 8 at 1:27
Wong Jia Hau
42448
42448
Not a good idea to copy past but how it works if you direct access to this d.ts from node_modules ?
– Gilsdav
Nov 8 at 8:04
@Gilsdav It will work as well without importing the actual module, however please remember to install the typings using--save-dev
options.
– Wong Jia Hau
Nov 8 at 8:19
add a comment |
Not a good idea to copy past but how it works if you direct access to this d.ts from node_modules ?
– Gilsdav
Nov 8 at 8:04
@Gilsdav It will work as well without importing the actual module, however please remember to install the typings using--save-dev
options.
– Wong Jia Hau
Nov 8 at 8:19
Not a good idea to copy past but how it works if you direct access to this d.ts from node_modules ?
– Gilsdav
Nov 8 at 8:04
Not a good idea to copy past but how it works if you direct access to this d.ts from node_modules ?
– Gilsdav
Nov 8 at 8:04
@Gilsdav It will work as well without importing the actual module, however please remember to install the typings using
--save-dev
options.– Wong Jia Hau
Nov 8 at 8:19
@Gilsdav It will work as well without importing the actual module, however please remember to install the typings using
--save-dev
options.– Wong Jia Hau
Nov 8 at 8:19
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199133%2ftypescript-types-for-code-that-is-not-imported-by-typescript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown