|
type F1 = (a: string, b:string) => void |
|
type F2 = (a: number, b:number) => void |
|
|
|
// remember & and | are intersection and union types respectively, intersection needs to be mutually-exclusive |
|
// don't simplify & and | to "and" and "or" |
|
const fn1: F1 & F2 = () => {} |
|
|
|
fn1('a', 'b') // passes |
|
fn1('a', 2) // fails |
|
fn1(1, 2) // passes |
|
|
|
// ^ not sure if this is a valid alternative of interface syntax of function overloading |
|
|
|
// --------------------- |
|
|
|
type LolThisPasses<A> = { |
|
[key in keyof A]: 's' |
|
} |
|
|
|
interface WtfThisFails<A> { |
|
[key in keyof A]: 's' |
|
} |
|
|
|
// this passes |
|
interface LooseType { |
|
[key: string]: any |
|
} |
|
|
|
// --------------------- |
|
|
|
type Hein<A> = { |
|
[key in keyof A]: 's' |
|
} |
|
|
|
const v1: Hein<'one' | 'two'> = 'one' // passes |
|
|
|
type WhatIsThisBehaviour<A> = { |
|
[key in keyof A]: 's' |
|
}[keyof A] |
|
|
|
const v3: WhatIsThisBehaviour<'one'> = 'mckmfk' // valid |
|
const v4: WhatIsThisBehaviour<'one'> = () => 'mckmfk' // also valid |
|
|
|
// --------------------- |
|
|
|
type Sizes = 'huuugge' | 'smol' |
|
type SizeMap = { |
|
[s in Sizes]: number |
|
} |
|
|
|
// oh both keys are required |
|
const sizes1: SizeMap = { |
|
huuugge: 9999, |
|
smol: 68+1, |
|
} |
|
|
|
// now they are optional |
|
const size2: Partial<SizeMap> = { |
|
huuugge: 9999, |
|
} |
|
|
|
// ---------------------------- |
|
|
|
interface Props<GenericData> { |
|
columns: Array<keyof GenericData>, |
|
data: GenericData[] |
|
} |
|
|
|
const Table = <T,>(props: Props<T>) => { |
|
return null |
|
} |
|
|
|
const data = [ |
|
{a: 1, b: '2'}, |
|
{a: 2, b: '22'}, |
|
] |
|
|
|
const app = <Table data={data} columns={["a"]} /> |
|
// ^ these are getting suggested |
|
|
|
// ---------------------------------------- |
|
|
|
// Labelled tuple elements |
|
type Range = [start: number, end: number]; |
|
|
|
// ---------------------------------------- |
|
|
|
const [,second] = l |
|
const [first,second, ,fourth] = l |
|
// [first, ...rest] -> rest element can only appear last |
|
|
|
// ---------------------- |
|
|
|
enum L { |
|
Up, |
|
Down = 7, |
|
Left, |
|
Right |
|
} |
|
|
|
console.log(L.Up); // 0 |
|
console.log(L.Left); // 8 |
|
// assign Down a string instead and TS will throw error for Left and Right |
|
|
|
// ----------------------- |
|
|
|
// Generic at interface level |
|
interface Oho<T> { |
|
a: T, |
|
b: (arg: T) => void |
|
} |
|
|
|
// Generic at function level |
|
interface Fn1<T> { |
|
(arg: T): void |
|
} |
|
|
|
// This should be same as above unless fn is overloaded |
|
interface Fn2 { |
|
<T>(arg: T): void |
|
} |
|
|
|
// A and B are same here, both have generic at function level |
|
type A = <U>(arg: U) => U |
|
type B = {<U>(arg: U): U} |
|
|
|
type C = {<U>(arg: U): void} |
|
|
|
const fn:C = () => {} |
|
// First line of this answer is very important here https://stackoverflow.com/a/62625899/7683365 |
|
|
|
// ----------------------- |
|
|
|
// for function overloading |
|
// this won't work |
|
interface Fn() { |
|
(v: number) |
|
(): number |
|
} |
|
|
|
function sum(initial: number) { |
|
let result = initial |
|
|
|
// but this will |
|
function fn(): number; |
|
function fn(v: number): typeof fn; |
|
|
|
function fn(v?: number) { |
|
if(v === undefined) return result |
|
result += v |
|
return fn |
|
} |
|
|
|
return fn |
|
} |
|
|
|
const v = sum(2)(2)(3)() |
|
|
|
// ----------------------------- |