JSFiddle - React, Tailwind, and code Playground
by jgarrido
TypeScript
/* const obj = {
foo: "bar",
name: "Jake",
another: "value",
hi: "there"
};
let newObj = {...obj, foo: "Bob"};
console.log(newObj);
*/
/* let p: [number, number];
let urmom: [string] = [''];
console.info(urmom);
urmom.push('mywang');
urmom.push(true);
urmom.push('randomguyswang');
console.info('what did I push into urmom?', urmom); */
/* type Pair<A, B, C, D> = [A, B, C, D];
const p: Pair<string, number, number, boolean> = ["age", 33, 22, true];
console.log(p); */
/* function lengthOf<T extends { length: number }>(x: T) {
return x.length;
}
lengthOf("abc"); // ok
lengthOf([1,2,3]); // ok
lengthOf({length: [123]}); // error */
//-- this is dumb, don't do it --//
/* let zipcodes: Array<T> = [];
zipcodes.push('hi', 'sup', '223');
const zipFunction = <T>(thing: T) :T => thing;
const zipFunction1 = <T>(thing: T): T => thing;
// console.log( zipFunction('bar') ); */
//-- shows how to define the 'T' of a generic --//
/* function makeArray<T>(...items: T[]): T[] {
return items;
}
const zipcodes = makeArray("03255", "02134"); // T inferred as string
console.info(zipcodes);
const nums = makeArray(3392,4323,2342343,5672190);
console.info(nums); */
//-- different function types --//
/* const sup: SupType = function urmom(a: string) {
return a;
}
type SupType = (arg0: string) => string;
const sup2: SupType = (a: string) => a;
console.log( sup('greetings') );
console.log( sup2('hi there') ); */
//-- tuples - setting a fixed number of elements --//
/* type Point = readonly [number, number];
const p: Point = [10, 20]; */
// console.info(p);
//-- without readonly, it's possible to push additional items to the array --//
/* let point: readonly [number, number] = [10, 20];
// point.push(50);
console.info(point); */
const points = [10, 20] as const; // type is readonly [10, 20] (very specific literals)
// points = [20, 30];
console.info(points);
// type stays...