-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implicit Referencing - opting in and opting out #1066
Comments
Hi @mhegazy, Thank you for the fully detailed response! Very helpful. First important take home:
Second important take home:
Thanks for detailing the 3 different ways context can be constructed. That's really helpful. I wasn't aware of the details of the third listed way:
I’d love you to clarify this a little if you could. It sounds like this scenario covers where someone is just using Visual Studio as an IDE for TypeScript. Presumably this means 1 of 2 things; either
Question about 1.If 1. is the case how does Visual Studio decide the compiler switches for TypeScript? What I mean is, even the Visual Studio TypeScript template solution ships with a csproj file that says Either way, the implication is that /// references should be used when using loose *.ts files to ensure context is built correctly. Question about
|
Question 1:
There are two options that you can control through tools/options (see screenshot below, namely:
Note that these configurations only affect loose files. for files in projects, you need to set them for every project. Question 2: Question 3: |
Thanks @mhegazy. That clarifies things a great deal.
This totally makes sense. However it raises an issue about having a TypeScript project which some people may be using Visual Studio to work on and some people may be using another IDE (eg WebStorm). In this situation it would be necessary to use /// references for the people not using Visual Studio. In order to avoid confusion in this situation the ideal would seem to be having a way of opting out of VS project support so everyone has the same experience regardless of IDE. So everyone would depend on using /// references to build context. I don't think there is a way to do this at present. Is this something that could made an option? eg. Have a Visual Studio setting that disabled implicit referencing? |
@jonathandturner did discuss the possibility of providing this option back on Oct 23, 2013 by the way: https://typescript.codeplex.com/workitem/1471#CommentContainer7 This discussion took a number of different turns after that and the option was never really returned to. Just thought I'd provide this as a reference. |
@johnnyreilly i am not sure i understand the desired behavior. is the problem the project file? what if you just forget about the project file and open the files in VS and let /// reference resolution figure out your context? if this is the case, just add after your import to Microsoft.TypeScript.targets. just note that it will ignore your project file all together. that means files, and configuration, and will treat all files as if they did not belong to a project. <PropertyGroup>
<!-- Indicates to the language service that this project supports TypeScript -->
<TypeScriptEnabled>false</TypeScriptEnabled>
</PropertyGroup> |
I'm guess the question is, what's the best way to set up a TypeScript project that can be worked on regardless of IDE (so whether it's Visual Studio, WebStorm etc)? As you say, you could have both the project file figuring out context for Visual Studio users and
For that reason I thought it might be simpler to abandon the project file approach entirely. That way the only maintenance is just the I appreciate this might sound like an odd thing to make a fuss about but I'm very keen to reduce "developer friction". When I've been introducing people to TypeScript I've had them complain that they feel the tooling is fighting against them on occasion. I want to reduce that as much as possible. Whilst using the project file to build context is a really handy shortcut for Visual Studio users it also kind of erects a barrier for TypeScript users between the Visual Studio world and those outside it. (I'm sure it wasn't intended to be that - but that's kind of a side effect I guess.) I'll try out the |
Hi @mhegazy, I put together a little test project using the https://github.com/johnnyreilly/proverb-without-implicit-referencing It has the csproj change you suggest:
I created a file I called And each TypeScript file has a So am I right in thinking that |
@johnnyreilly, increasing developer productivity is the reason typescript exists, so friction is not something desired. The part that I find confusing is why you need the project file. If you just open one of your files by itself, /// references will cause you to load the all your files, similar to other editors. I am assuming you are using gulp to build anyways. Either ways, figuring out files is not the only issue, compiler options are the other piece of the puzzle. What tells the language service is to not to load anything from the project file. |
Yes I am using Gulp to build. The project file is kind of extraneous with this approach. I suppose I'd imagined that someone using Visual Studio would automatically be using a project file as they would in a non-TypeScript project. And if they were, I was keen to see if there was a way to use the project file without opting into the implicit file referencing. I think we've established this is possible with Though you raise an interesting point - I'm guessing you're thinking that if the user is using Visual Studio just as an editor that they'd rely on the TypeScript Virtual Project instead? That way they can entirely bin the project file. As you rightly say, compiler options are the other piece of the puzzle. From what you've said earlier, without a project file you have no control over compiler options. That's a shame - I'm a From what I understand, a cross IDE project using a common approach is pretty much do-able in the following ways:
Are there any plans to allow the control of "default" TypeScript compiler options in Visual Studio in the absence of a project file? This would help with approach 3. I think approach 1 might be the best cross-IDE approach available at present in my view. |
@johnnyreilly i think you summed up nicely. thanks for the write up. I do not see a problem with us exposing more options as global settings for loose files, e.g. --noImplicitAny, and --target. I have created #1223 to track that. but there are ones that we know we can not support globally, e.g. out, outDir, declarations. maproot, sourceRoot. There is room here for a proposal :) |
A good counter example is GitHub-Electon or similar background-worker script/dom-script situations where definintions might only be available in certain contexts. For Electron, there are two modules called 'ipc' that are loaded in different contexts only ("renderer" vs "main" contexts). With Typescript automatically including ALL available files in the project, I cannot be selective about which d.ts contexts to include for a particular script. Instead I have to split it into multiple projects - maybe not a bad thing, I'm not sure. (also, the 'ipc' conflict doesn't appear in the GitHub-Electon*.d.ts files in their current release, but would once those definitions mature) |
@GITGIDDY tsconfig.json (with full support in next VS2015 public release) should solve this issue for you. |
@mhegazy I've just come across this problem, and after finding multiple issues on the subject this seems to be the most appropriate place to get some clarification. First let me explain my situation a bit: I've got a Visual Studio project, with its TypeScript properties set to combine the compiled TypeScript into a single .js file, and among many other .ts files it has a base class in one .ts file and a child class in another .ts file. Until I added the base class I had assumed that I didn't need to use /// references at all while using a Visual Studio project, because I thought Visual Studio was figuring out TypeScript dependencies on its own. After reading this issue I now realise that it's not, and in my case my output .js file has the base class declared after the child class, resulting in the "TypeError: b is undefined in __extends" error (#4341). So, based on your responses to this and other issues, it sounds like I should use a _references.ts file or just use /// reference directives in my source files. The problem I have with these options is that they're manual processes, but the overall issue seems to be something that could have an automated solution. For example, when you described this process (regarding how files are passed to the TypeScript compiler by Visual Studio):
Wouldn't it be possible for an extra step to be added here so that, instead of initially ordering the files based on their order within the project file, the initial ordering of the files is based on their dependencies? I mean, doesn't Visual Studio already have this information at hand? (I assume this because IntelliSense and Go To Definition work in TypeScript, so it must surely be quite easy to figure out that a file containing a base class needs to be compiled before files containing child classes). Having said that though, I realise that in reality there's probably a huge amount of work involved in implementing this dependency checking and ordering functionality – and if it were to be implemented it would probably belong in the TypeScript compiler itself, not in Visual Studio. So I guess my only real point of confusion is, why does Visual Studio do anything around a "compilation context" at all? I guess I'm with @johnnyreilly when he says:
The current behaviour just seems a bit misleading to me. The way that Visual Studio figures out the TypeScript references without explicitly having any /// directives, and then enables IntelliSense etc, makes it appear as though your code will run when it won't necessarily. This mainly seems to be an issue around inheritance, because of how I realise this whole comment probably seems a bit aimless, so I guess my actual point can be boiled down to this: If you are working in a project within Visual Studio, and:
Then there is a high likelihood that you will still need to use /// reference directives or a _references.ts file in order to have your output JavaScript run without errors. With that being the case, wouldn't it be better to require developers to use /// reference directives within Visual Studio (like they have to when using other IDEs) rather than giving the appearance of handling this automatically? I realise I'm simply repeating @johnnyreilly's point, and like him I understand that it might seem like a small thing to be fussing about, but I do think it's misleading the way things are. Am I missing something obvious, or would I be right in saying that there's not really anything gained by having Visual Studio detect the TypeScript compilation context? |
It is a design choice not to reorder output. the compiler will generate code that looks like your input. The compiler can not generate code that will always work in all cases, e.g. if you have a cyclic dependency between files. The solution we believe is correct is to issue an error when you have your base before your derived, see #5207, the PR for it is in, #4343, we need to update the PR though. |
Thanks for the quick reply, I don't want to keep bothering you about this for too long, but if it's a design choice to not reorder the output, what was behind the design choice to have all of a project's .ts files automatically passed to the TypeScript compiler (in project reference order) in the first place? Would I be right in saying that this was because there used to be no support for a tsconfig.json file (before TypeScript 1.5)? So it was the only way to have Visual Studio compile TypeScript files in a project automatically? I guess the alternative would have been for the user to add a pre or post build step to run tsc.exe, which wouldn't have been very nice. If that's the case, will things be moving more towards tsconfig.json in the future (even when using a project file)? I've been reading through #3983 and its related issues. It kind of sounds to me like the TypeScript properties in a *proj file are on their way out, and instead we will always be using tsconfig.json in future. This is certainly already the case for ASP.NET 5 projects, so is this the intent for other kinds of projects as well? |
Well. the ts files need to be compiled to js files to be able to run, we would assume you want to compile all the files in your project, so we pass them all to the compiler. this is the same behavior as C#, VB, etc..
yes. this is the intention. tsconfig.json has been easier for developers to reason about than the xml project file, and fits with other JS tooling patterns.
Starting with TS 1.8, if you have a tsconfig.json in your project file it will be used as the source of configurations for all project types. see https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#improved-support-for-tsconfigjson-in-visual-studio-2015 |
I can confirm that |
Excellent, thanks for clearing that up. I didn't realise that tsconfig.json was already supported for all project types - I should have just tried it. I'm already using TypeScript 1.8, so I'll switch to using tsconfig.json right away. Thanks for your help. |
…ided, closes #117, bump 4.0.6
@mhegazy |
Hi,
I've a question about Visual Studio's Implicit Referencing. I've looked around in vain for a definitive answer and so I thought it might make sense to raise the question here.
Since 0.9.1 I've used this feature pretty happily and purged my files of the mass of
/// <reference path=
entries.I've recently been wondering if there are any hidden "gotchas" to using
/// <reference path=
in a Visual Studio context? My understanding is that if there are any/// <reference path=
instances in a file then implicit referencing goes out the window and only the specified references are used. Is that correct? (So use of any references in the head of ats
file is effectively the same as opting that file out of Implicit Referencing for Visual Studio.)@mhegazy - this might be a question for you? Similar ground was covered here.
The text was updated successfully, but these errors were encountered: