2

How can I make TypeScript aware that the Variable is defined in the cshtml file? Can I add a reference inside the ts file?

1 Answer 1

5

You can do this in a TypeScript file or a .d.ts file:

declare var fromMvc: string;

This doesn't produce any code, but it makes the compiler aware that a variable with the name "fromMvc" exists. After declaring that you can use it like a normal variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I've also found that if the variable is defined in another JS it's possible to write: /// <reference path="../typings/jquery/jquery.d.ts" />
You are right, when it is defined somewhere else you can use the reference tag. When you are in Visual Studio and the .d.ts is part of your project (with TypeScriptCompile as build action) you don't need the references tag (normally, you might need it for ordering when using single output as an option)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.