ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[TypeScript] Model a DataStore type

2022-07-13 19:03:11  阅读:161  来源: 互联网

标签:TypeScript song movie DataEntityMap data export Model type id


Given this code as starter:

export interface DataEntity {
  id: string
}
export interface Movie extends DataEntity {
  director: string
}
export interface Song extends DataEntity {
  singer: string
}
 
export type DataEntityMap = {
  movie: Movie
  song: Song
}
 
export class DataStore {}

This DataEntityMap object should drive a lot of what happens to DataStore.

Ultimately, DataStore should end up with methods like

const ds = new DataStore()
ds.addSong({ id: "song-123", singer: "The Flaming Lips" })
ds.addMovie({
  id: "movie-456",
  director: "Stephen Spielberg",
})
ds.getSong("song-123") // returns the song
ds.getMovie("movie-456") // returns the movie
ds.getAllSongs() // array of all songs
ds.getAllMovies() // array of all movies
ds.clearSongs() // clears all songs
ds.clearMovies() // clears all movies

It’s ok to define these explicitly in the DataStore class, but they should be type-checked against the DataEntityMap type in some way.

Requirements

  • If you mis-name a method on the class (e.g., getSongs instead of getAllSongs), you should get some sort of type error that alerts you that you’ve broken the established pattern
  • If you add a new entity like Comic (shown below) and make no other changes to your solution, you should get some sort of type error that alerts you to the absence of a clearComicsgetAllComics and getAllSongs method.

  • There should be no externally-visible properties on an instance of DataStore beyond the required methods
  • Your code, and the test suite should type-check

.

.

.

.

.

.

.

.

Answer

export interface DataEntity {
  id: string
}
export interface Movie extends DataEntity {
  director: string
}
export interface Song extends DataEntity {
  singer: string
}

export interface Comic extends DataEntity {
  issueNumber: number
}

export type DataEntityMap = {
  movie: Movie
  song: Song
}

/**
 * if you change DataEntityMap from type to interface
 * Then you can have the DataEntityMap defined in multi files and TS will combine those automaticlly
// model/song.model.ts
export interface DataEntityMap {
  song: Song;
}  
// model/movie.model.ts
export interface DataEntityMap {
  movie: Movie;
}
 */

export type DataStoreMethods = {
  [K in keyof DataEntityMap as `getAll${Capitalize<K>}s`]: () => DataEntityMap[K][]
} & {
  [K in keyof DataEntityMap as `get${Capitalize<K>}`]: (id: string) => DataEntityMap[K]
} & {
  [K in keyof DataEntityMap as `clear${Capitalize<K>}s`]: () => void
}
& {
  [K in keyof DataEntityMap as `add${Capitalize<K>}`]: (item: DataEntityMap[K]) => DataEntityMap[K]
}

function isDefined<T>(x: T | undefined): x is T {
  return typeof x !== "undefined";
}

export class DataStore implements DataStoreMethods {
  #data: {[K in keyof DataEntityMap]: Record<string, DataEntityMap[K]>} = {
    movie: {},
    song: {}
  }

  getSong(id: string): Song {
    const song =  this.#data.song[id];
    if (!song) {
      throw new Error(`Could not find a song with id ${id}`);
    }
    return song;
  }
  getAllSongs(): Song[] {
    return Object.keys(this.#data.song).map(id => this.#data.song[id]).filter(isDefined)
  }
  clearSongs(): void {
    this.#data.song = {}
  }

  addSong(song: Song) {
    this.#data.song[song.id] = song;
    return song;
  }

  getMovie(id: string): Movie {
    const movie =  this.#data.movie[id];
    if (!movie) {
      throw new Error(`Could not find a movie with id ${id}`);
    }
    return movie;
  }
  getAllMovies(): Movie[] {
    return Object.keys(this.#data.movie).map(id => this.#data.movie[id]).filter(isDefined)
  }
  clearMovies(): void {
    this.#data.movie = {}
  }
  addMoive(movie: Movie) {
    this.#data.movie[movie.id] = movie;
    return movie;
  }
}

 

标签:TypeScript,song,movie,DataEntityMap,data,export,Model,type,id
来源: https://www.cnblogs.com/Answer1215/p/16475167.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有