glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
11.77k stars 1.04k forks source link

fix(ts-input): Replace topLevelName for TypeScript input if first type is export default #2550

Closed inferrinizzard closed 2 months ago

inferrinizzard commented 3 months ago

Description

Fixes an issue with type naming for TypeScript input where the name ends up as default if the first type in the file is export default. Replaces the topLevelName with the name from the corresponding ref type

Motivation and Context

Fixes #1295

Input File

/** #TopLevel */
export default class AddressBook {
  /**
   * A dictionary of Contacts, indexed by unique ID
   */
  contacts: { [id: string]: Contact };
}

class Contact {
  firstName: string;
  lastName?: string;

  birthday?: Date;
  title?: "Mr." | "Mrs." | "Ms." | "Prof.";

  emails: string[];
  phones: PhoneNumber[];

  /** @TJS-type integer */
  highScore: number;
}

/**
 * A Contact's phone number.
 */
class PhoneNumber {
    number: string;

    /** An optional label (e.g. "mobile") */
    label?: string;
}

Previous Behaviour / Output

import 'dart:convert';

class Default {
    Map<String, Contact> contacts;

    Default({
        this.contacts,
    });

    factory Default.fromJson(String str) => Default.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory Default.fromMap(Map<String, dynamic> json) => new Default(
        contacts: new Map.from(json["contacts"]).map((k, v) => new MapEntry<String, Contact>(k, Contact.fromMap(v))),
    );

    Map<String, dynamic> toMap() => {
        "contacts": new Map.from(contacts).map((k, v) => new MapEntry<String, dynamic>(k, v.toMap())),
    };
}

New Behaviour / Output

import 'dart:convert';

AddressBook addressBookFromJson(String str) => AddressBook.fromJson(json.decode(str));

String addressBookToJson(AddressBook data) => json.encode(data.toJson());

class AddressBook {

    ///A dictionary of Contacts, indexed by unique ID
    Map<String, Contact> contacts;

    AddressBook({
        required this.contacts,
    });

    factory AddressBook.fromJson(Map<String, dynamic> json) => AddressBook(
        contacts: Map.from(json["contacts"]).map((k, v) => MapEntry<String, Contact>(k, Contact.fromJson(v))),
    );

    Map<String, dynamic> toJson() => {
        "contacts": Map.from(contacts).map((k, v) => MapEntry<String, dynamic>(k, v.toJson())),
    };
}

How Has This Been Tested?

TBD on how we should test quicktype-typescript-input

Screenshots (if appropriate):