Skip to content
⚠️ This article was written in 2019. Some content may be outdated.

TypeScript 3.4 New Features at a Glance

TypeScript 3.4 new features are topics encountered frequently in day-to-day development. This article draws from real projects to share practical implementation approaches and lessons learned.

Core Principles

Here is a basic usage example:

javascript
import React, { Component } from "react";

class DataList extends Component {
  state = { items: [], loading: true };

  async componentDidMount() {
    const res = await fetch("/api/items");
    const items = await res.json();
    this.setState({ items, loading: false });
  }

  render() {
    const { items, loading } = this.state;
    if (loading) return <div>Loading...</div>;
    return (
      <ul>
        {items.map((item) => (
          <li key={item.id}>{item.name}</li>
        ))}
      </ul>
    );
  }
}

This pattern is concise and suitable for most scenarios.

Source Analysis

Here is the core code:

javascript
import React, { Component } from "react";

class Form extends Component {
  state = { name: "", email: "", errors: {} };

  handleChange = (e) => {
    this.setState({ [e.target.name]: e.target.value });
  };

  handleSubmit = (e) => {
    e.preventDefault();
    const errors = this.validate();
    if (Object.keys(errors).length === 0) {
      this.props.onSubmit(this.state);
    } else {
      this.setState({ errors });
    }
  };

  validate() {
    const errors = {};
    if (!this.state.name) errors.name = "Required";
    if (!this.state.email.includes("@")) errors.email = "Invalid format";
    return errors;
  }

  render() {
    return <form onSubmit={this.handleSubmit}>...</form>;
  }
}

In real projects, you also need to consider edge cases and error handling.

Practical Application

Here is a real-world example:

javascript
function deepClone(obj, map = new WeakMap()) {
  if (obj === null || typeof obj !== "object") return obj;
  if (map.has(obj)) return map.get(obj);

  const clone = Array.isArray(obj) ? [] : {};
  map.set(obj, clone);

  for (const key of Object.keys(obj)) {
    clone[key] = deepClone(obj[key], map);
  }
  return clone;
}

After promoting this pattern across the team, the results were great and maintenance costs dropped noticeably.

MIT Licensed