What is the standard for use of quotes in Typescript?
There is no particular standard to use single quotes for characters and double quotes for string but it is suggested to use double quotes for strings and vice versa.
From the docs:
Just like JavaScript, TypeScript also uses the double quote (") or single quote (') to surround string data.
I would go with single quotes. I pretty much agree with this guy:
- Prefer single quotes (') unless escaping.
Reason: More JavaScript teams do this (e.g.airbnb, standard, npm, node, google/angular, facebook/react). Its easier to type (no shift needed on most keyboards).
Prettier team recommends -
single quotes as welldouble quotes
Also, even dotnet new
templates use single quotes for Angular apps.
The coding standards document as linked by @crowebird is a good document: https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines
I like all the guidelines except for the double quotes - when it comes to using typescript with Angular 2.
This question isn't about Typescript with Angular 2, but readers may be Angular 2 users. Using single quotes makes for easier reading when marking up html strings in the typescript.
Take the following example:
@Component({
...,
template: '<div class="some-class-name"></div>'
})
But if you are using double quotes you have to escape the double quotes:
@Component({
...,
template: "<div class=\"some-class-name\"></div>"
})
The first option is preferable. Most of the Angular 2 demos use single quotes.
This was the first result in my google search for: "double vs single quotes typescript."
Considering the accepted answer is a little old (but still valid from the docs) I would like to add this quote from: https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines updated on November 27, 2015:
Use double quotes for strings.
Granted "the code is more what you'd call 'guidelines' than actual rules." :)