Ezward
Ezward

Reputation: 17702

Targeting ES5 with TypeScript in Visual Studio

I would like to use get/set syntax in TypeScript within Visual Studio Express for Web. How do I enable this. I currently get this error when compiling;

Property accessors are only available when targeting ES5 or greater

The file being compiled has a build action of TypeScriptCompile. I don't know how to add a the necessary compiler switch from within Visual Studio.

Any help would be appreciated.

Upvotes: 31

Views: 21832

Answers (5)

Devon Holcombe
Devon Holcombe

Reputation: 528

I am using Visual Studio 2013 Update 4 with Web Essentials. Microsoft has made changing the targetted ECMAScript version much easier.

You can now do the following:

  1. Right click your project name and click Properties.
  2. In the Properties window select "Typescript Build"
  3. Set ECMAScript version to "ECMAScript 5".

I believe ECMAScript 5 is currently the default. You can at present also choose ECMAScript 3 or ECMAScript 6 as targets.

Upvotes: 5

Ian Boyd
Ian Boyd

Reputation: 256731

This has changed with TypeScript 0.8.2. You now change TypeScriptTarget in the .csproj file from:

<TypeScriptTarget>ES3</TypeScriptTarget>

to

<TypeScriptTarget>ES5</TypeScriptTarget>

MyApp.csproj:

  <PropertyGroup Condition="'$(Configuration)' == 'Debug'">
    <TypeScriptTarget>ES5</TypeScriptTarget>
    <TypeScriptIncludeComments>true</TypeScriptIncludeComments>
    <TypeScriptSourceMap>true</TypeScriptSourceMap>
    <TypeScriptModuleKind>AMD</TypeScriptModuleKind>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <TypeScriptTarget>ES5</TypeScriptTarget>
    <TypeScriptIncludeComments>false</TypeScriptIncludeComments>
    <TypeScriptSourceMap>false</TypeScriptSourceMap>
    <TypeScriptModuleKind>AMD</TypeScriptModuleKind>
  </PropertyGroup>

See also Asher Barak answer

Upvotes: 25

user1110435
user1110435

Reputation: 31

Using Studio 2012, project template type TypeScript the build, in the project csproj file is set to ES3

ES3

Change it to ES3 to ES5, save it and reload the project.

Upvotes: 1

Gamer
Gamer

Reputation: 19

The switch for instructing the TSC.EXE to generate ES5 compatible code is --target ES5 (note the double dashes).

Each project has a file called [Something].csproj (C# project in our case). Open that file using notepad and look for Target xml element. Change the exec command by adding the --target ES5.

Before:

 <Exec Command="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript\0.8.0.0\tsc&quot; @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />

After:

<Exec Command="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript\0.8.0.0\tsc&quot; --target ES5 @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />

Upvotes: 2

mohamed hegazy
mohamed hegazy

Reputation: 9509

You need to pass the -target ES5 to the compiler. The compilation is triggered using an msbuild task in your project file. Your project file probably has a "TypeScriptCompile" target like the onr bellow, just make sure to the target argument is passed. Here is an example:

<Target Name="TypeScriptCompile" BeforeTargets="Build">
   <Message Text="Compiling TypeScript files" />
   <Exec Command="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript\0.8.0.0\tsc&quot; -target ES5 @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
</Target>

Upvotes: 22

Related Questions