Skip to content

Instantly share code, notes, and snippets.

@robertcoopercode
Created January 9, 2019 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertcoopercode/a4d8d492015bccf762fd2452704b1e12 to your computer and use it in GitHub Desktop.
Save robertcoopercode/a4d8d492015bccf762fd2452704b1e12 to your computer and use it in GitHub Desktop.
const fillArray = <T>(len: number, elem: T) => {
return new Array<T>(len).fill(elem);
};
const newArray = fillArray<string>(3, 'hi'); // => ['hi', 'hi', 'hi']
newArray.push('bye'); // ✅
newArray.push(true); // ❌ - only strings can be added to the array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment