31

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.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Ezward
  • 17,327
  • 6
  • 24
  • 32

5 Answers5

25

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

Community
  • 1
  • 1
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
22

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>
mohamed hegazy
  • 9,191
  • 4
  • 30
  • 21
5

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.

Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
Devon Holcombe
  • 518
  • 1
  • 5
  • 18
2

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;', ' ')" />
Gamer
  • 19
  • 2
  • Here is what I ended up with. This includes flags for the ES5 target, AMD modules and sourcemaps. Basically, the stuff you want for browser development. – Ezward Oct 15 '12 at 21:45
1

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.

user1110435
  • 31
  • 1
  • 2