Skip to content

Instantly share code, notes, and snippets.

@robertcoopercode
Created January 14, 2019 01:18
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/e89ad136edc735992d121a3aeb723674 to your computer and use it in GitHub Desktop.
Save robertcoopercode/e89ad136edc735992d121a3aeb723674 to your computer and use it in GitHub Desktop.
const fillArray = <ArrayElementType>(len: number, elem: ArrayElementType) => {
return new Array<ArrayElementType>(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