class Class Basics (accessors & static members)

Last updated: 2026-10-22

class Class Basics (accessors & static members)

A class bundles data and behavior: fields hold state, the constructor initializes, methods define behavior. new creates instances and this refers to the current one. TS layers field type annotations and compile-time checks on top of JS classes, so missing initializers and wrong arguments surface while you type.

GroupFunctions & Classes
KindSyntax
Strict modeNot required

ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.

📝 Syntax

class Book {
  title: string;   // field: explicit type
  author: string;
  copies: number;

  constructor(title: string, author: string, copies = 1) {
    this.title = title;
    this.author = author;
    this.copies = copies;
  }

  describe(): string {
    return `${this.title} - ${this.author}`;
  }
}

const book = new Book("The Three-Body Problem", "Cixin Liu", 3);

⚙️ Parameters

constructorConstructor: runs on new; responsible for initializing every field
describe(): stringMethod: behavior living on the prototype; return type annotated explicitly

Returns:Methods get explicit return types; the constructor takes none (it implicitly returns the new instance).

▶ Example

Edit the code below and click Try it to see the transpiled result:

Output:

The Three-Body Problem - Cixin Liu (3 copies)

(Runtime output after transpilation; new Book("The Three-Body Problem", 123) is a compile error — the second argument must be string)

💡 Notes

ℹ️ Info Fields can be initialized at the declaration site (copies: number = 1), eliminating constructor assignments; the type can be inferred from the initializer.
💡 Tip Parameter properties are a TS feature: constructor(public title: string) declares the parameter and creates/initializes the field in one step, equivalent to this.title = title.
⚠️ Note With strictPropertyInitialization on, fields never assigned at declaration or in the constructor error; nullable fields take string | undefined, later-assigned ones use definite assignment (!).
More cases coming soon.

📚 Related tutorials

❓ FAQ

Q Can field types be omitted?
A Yes when initialized at declaration (copies = 1 infers number); a bare declaration must be annotated or it is implicitly any.
Q Does the constructor have a return type?
A You neither write nor may write one — it implicitly returns the class instance. TS does not allow a return type annotation on a constructor; the type of a new expression is always the class instance.
Q What if this is lost inside a callback?
A Use arrow-function fields (say = () => this.x) or bind before passing; TS also supports a this parameter (fn(this: Book)) for compile-time enforcement.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏