Created
February 7, 2020 16:50
-
-
Save lancegliser/a17f78c43c185b35788a7832afe146b6 to your computer and use it in GitHub Desktop.
An example of creating a wrapping object that allows for data property only (without function) partial constructors for nested classes with functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type NonFunctionPropertyNames<T> = { | |
[K in keyof T]: T[K] extends Function ? never : K; | |
}[keyof T]; | |
type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>; | |
type PartialWithoutFunctions<T> = Partial<NonFunctionProperties<T>>; | |
interface IKeywordsResponseArguments { | |
results?: PartialWithoutFunctions<KeywordResult>[]; | |
} | |
export class KeywordsResponse extends ResultsResponse { | |
public results: KeywordResult[] = []; | |
public constructor(data: IKeywordsResponseArguments = {}) { | |
super(data); | |
if (data.results) { | |
this.results = data.results.map(result => new KeywordResult(result)); | |
} | |
} | |
} | |
export class KeywordResult { | |
public title = ""; | |
public content = ""; | |
public sentiment = 0; | |
public names = ""; | |
public places = ""; | |
public keywords = ""; | |
public link = ""; | |
public language = ""; | |
public publishDate = ""; | |
public constructor(data: Partial<StreamingSentimentResult> = {}) { | |
Object.assign(this, data); | |
} | |
public getNames(): string[] { | |
return getValuesFromPipeDelimitedString(this.names); | |
} | |
public getPlaces(): string[] { | |
return getValuesFromPipeDelimitedString(this.places); | |
} | |
public getKeywords(): string[] { | |
return getValuesFromPipeDelimitedString(this.keywords); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment