Types vs Interfaces: What I Got Wrong In 2022

Matt Pocock
18 Apr 202509:57

Summary

TLDRIn this video, the speaker reflects on their evolving views regarding TypeScript, particularly the debate between types and interfaces. They recount their past mistakes, including incorrect performance assumptions and the implications of using intersections vs. extends in TypeScript. Through personal benchmarks and insights from the TypeScript team, the speaker concludes that while performance matters, the choice between types and interfaces is often not critical. The final recommendation is to default to interfaces unless a specific feature of types is needed, with an emphasis on maintaining performance as TypeScript evolves.

Takeaways

  • 😀 The speaker revisits their previous content on TypeScript's **types** vs **interfaces**, acknowledging they made some incorrect statements in the past.
  • 😀 The TypeScript performance wiki doesn’t state that **types** are slower than **interfaces**; instead, it compares **interface extends** vs **intersection types** in terms of performance.
  • 😀 **Interfaces** are faster than **intersection types** when dealing with hierarchical relationships, particularly when properties from multiple types are merged.
  • 😀 **Declaration merging** in interfaces can lead to tricky situations, but can be mitigated by using TypeScript ESLint rules to prevent unsafe merges.
  • 😀 The speaker conducted a benchmark comparing types and interfaces, but the results didn’t show a significant performance difference when used in normal object typing.
  • 😀 **Consistency** in using types or interfaces across a project is important, but neither is inherently superior for basic object typing in TypeScript.
  • 😀 **Interfaces** are best for extending existing types and adding new properties to an object, while **types** are often sufficient for basic object definitions.
  • 😀 When working with **interfaces**, be aware of the potential issue with **declaration merging**, where multiple declarations of the same interface can cause unexpected behavior.
  • 😀 **Types** are useful for situations where **interfaces** cannot be used, such as when needing to define more complex type structures or constraints.
  • 😀 The recommendation has shifted to **default to interfaces** for most use cases, unless a feature specific to types is needed, particularly to avoid performance degradation.
  • 😀 The speaker reflects on their changing views, noting that once you’ve flip-flopped multiple times, the important thing is that **interfaces extend over intersections** for better performance in some cases, but for simple object typing, it doesn't matter which you choose.

Q & A

  • What was the main point of the video?

    -The main point of the video was to revisit the creator's past opinions about Typescript, specifically around the debate of using 'types' versus 'interfaces'. The creator reviews how their perspective has evolved over time and clarifies misconceptions they had in the past.

  • What was the first mistake the creator made in their past video?

    -The creator mistakenly claimed that 'types' were slower than 'interfaces' based on performance. They clarified that this wasn't true, especially when considering how intersections and extensions work in TypeScript.

  • What is the correct interpretation of the TypeScript performance wiki regarding interfaces and types?

    -The TypeScript performance wiki actually contrasts 'interfaces' with 'intersections', not 'types' with 'interfaces'. The wiki suggests that interfaces can perform better in specific scenarios, especially when they have a hierarchical structure, compared to using intersections of types.

  • Why are 'interfaces' faster than 'types' in some cases?

    -In cases where you are combining multiple object types, interfaces can perform faster because they use 'extends', which allows TypeScript to check types hierarchically. This is easier for TypeScript to compile than an intersection, which might have conflicts or complexities.

  • What did the creator's benchmark study reveal?

    -The creator's benchmark study found that there was no significant performance difference between 'types' and 'interfaces' when used in isolation. However, intersections of types could degrade performance, especially in larger codebases.

  • What does the creator recommend when using 'types' versus 'interfaces'?

    -The creator’s final recommendation is to default to using 'interfaces' unless you need a specific feature that types provide. Previously, they had recommended 'types' by default, but after further reflection and research, they now believe interfaces are the better default.

  • What is declaration merging in TypeScript, and how does it work?

    -Declaration merging in TypeScript refers to the ability to define the same interface multiple times and have them combine into one. This can be useful but also problematic if not managed carefully, as it can lead to unintended overrides and bugs.

  • How can declaration merging issues be mitigated?

    -Declaration merging issues can be mitigated by using the ESLint rule 'no unsafe declaration merging'. This will alert you to any dangerous merging operations, even if TypeScript allows it.

  • What is the issue with the implicit index signature in 'types'?

    -The implicit index signature in 'types' means that 'types' can have properties of any key with an unknown value type. This behavior doesn't match interfaces, where the properties are fixed unless explicitly declared otherwise, causing issues when types are used in situations where strict matching is required.

  • What is the takeaway about TypeScript performance from the creator's reflection?

    -The creator emphasizes that TypeScript's performance can still be affected by the way types and interfaces are used, especially when creating large intersections. Even with improvements in TypeScript's speed, it's important to be mindful of how types are combined to avoid performance degradation.

Outlines

plate

Dieser Bereich ist nur für Premium-Benutzer verfügbar. Bitte führen Sie ein Upgrade durch, um auf diesen Abschnitt zuzugreifen.

Upgrade durchführen

Mindmap

plate

Dieser Bereich ist nur für Premium-Benutzer verfügbar. Bitte führen Sie ein Upgrade durch, um auf diesen Abschnitt zuzugreifen.

Upgrade durchführen

Keywords

plate

Dieser Bereich ist nur für Premium-Benutzer verfügbar. Bitte führen Sie ein Upgrade durch, um auf diesen Abschnitt zuzugreifen.

Upgrade durchführen

Highlights

plate

Dieser Bereich ist nur für Premium-Benutzer verfügbar. Bitte führen Sie ein Upgrade durch, um auf diesen Abschnitt zuzugreifen.

Upgrade durchführen

Transcripts

plate

Dieser Bereich ist nur für Premium-Benutzer verfügbar. Bitte führen Sie ein Upgrade durch, um auf diesen Abschnitt zuzugreifen.

Upgrade durchführen
Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
TypeScriptTypes vs InterfacesPerformanceDevelopment TipsCoding Best PracticesProgrammingTech AdviceSoftware EngineeringWeb DevelopmentTypeScript PerformanceCode Optimization
Benötigen Sie eine Zusammenfassung auf Englisch?