sindresorhus / camelcase-keys

Convert object keys to camel case
MIT License
679 stars 92 forks source link

camelcaseKeys return key types on camelcase without exclude prop. #83

Open serjo96 opened 2 years ago

serjo96 commented 2 years ago

I have class with types for my object and I using convert to camel case like that with exclude keys:

camelcaseKeys(data, { exclude: ['iso_639_1', 'iso_3166_1'], deep: true });

Data after formatting camelcaseKeys is normal, but ts see like iso keys on camel cases to iso6391. image

export declare class MovieDetails {
  id: number;
  budget: number;
  imdb_id: string | null;
  images: Images
  genres: Genre[];
  spoken_languages: Language[];
  overview: string | null;
  poster_path: string | null;
  production_companies: Company[];
  production_countries: Country[];
  revenue: number;
  runtime: number;
  status: string;
  tagline: string;
  belongs_to_collection: Collection | null;
  title: string;
  homepage: string | null;
  original_title: string;
  adult: boolean;
  release_date: string;
  keywords: {
    keywords: Keyword[];
  };

  credits: MovieCredits;
  lists: ListData<MovieListItem>;
  videos: Videos;
  recommendations: ListData<MoviesListItem>;
  backdrop_path: string;
  original_language: string;
  popularity: number;
  vote_count: number;
  video: boolean;

  vote_average: number;
}

export declare class Language {
  iso_639_1: string;
  name: string;
}

export declare class Images {
  backdrops: Image[];
  posters: Image[];
  logos: Image[];
}

export declare class Image {
  aspect_ratio: number;
  file_path: string;
  height: number;
  iso_639_1: string | null;
  vote_average: number;
  vote_count: number;
  width: number;
}

and etc...

Maybe add some generic only for return type, not only one generic for input and return type? image

sindresorhus commented 2 years ago

// @g-plane

yutak23 commented 1 year ago

I think as const is required. (add as const , then your ['iso_639_1', 'iso_3166_1'] can be of type ['iso_639_1', 'iso_3166_1'], rather than string[])

Refer to: https://github.com/sindresorhus/camelcase-keys/blob/v8.0.2/index.test-d.ts#L84

TS reference : https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions