Skip to content

Instantly share code, notes, and snippets.

@cenkce
cenkce / tuple-operators-examples.ts
Last active February 23, 2020 08:44
Tuple Operators Examples
// ... import types
type ComponentProps: {
EnvironmentVariable: TestListToUnion // [] | ["key1", "union type 1" | "union type 2" | "union type 3"] | ["key2", string] | ["key3", number] ...
EnvironmentVariables: TestListToTypeAlias // {key4: "type of string";} & {key3: number;} ...
SelectedEnvironmentVariableKey: TestKeys // "key1" | "key2" | "key3" | "key4" | undefined
...
}
function Component(props: ComponentProps) {
@cenkce
cenkce / tuple-test.ts
Last active February 23, 2020 08:42
Test of Tuple Operators
type TestTuple = [
"key1",
"union type 1" | "union type 2" | "union type 3",
"key2",
string,
"key3",
number,
"key4",
[string, number, null | string]
];
@cenkce
cenkce / tuple-operators.ts
Created February 22, 2020 23:08
Typescript Tuple Operators
type SliceTuple<
T extends any[],
TMaxItem extends number = 2,
R extends any[] = [],
P extends any[] = [],
I extends any[] = []
> = {
0: SliceTuple<T, TMaxItem, R, Prepend<T[Pos<I>], P>, Next<I>>;
1: Reverse<Prepend<Reverse<P>, R>>;
2: SliceTuple<T, TMaxItem, Prepend<Reverse<P>, R>, [T[Pos<I>]], Next<I>>;
// ------- Typing Operators -------
// https://github.com/pirix-gh/medium/blob/master/types-curry-ramda/src/index.ts
export type Iterator<
Index extends number = 0,
From extends any[] = [],
I extends any[] = []
> = {
0: Iterator<Index, Next<From>, Next<I>>;
1: From;
const someVariable: {
prop1: "value1",
props2: "value2",
nestedType: {
nestedProps: "nested value",
...
},
...
}
// Extracts only non nested values
type ExtractValue<T extends {[key:string]: any}> = T extends {[key:string]: any} ? T[keyof T] : T;
// Recusively extracts values
type ExtractValues<T extends {[key:string]: any}> = T extends {[key:string]: any} ? ExtractValue<T[keyof T]> : never;
type ValuesOfComplexType = ExtractValues<ComplexType>;
// or
type ValuesOfComplexType = ExtractValues<typeof ComplexConstants>;
// assings the values
// "any value 1" | "any value 2" | "any value 3" | "any value 4" ...
type ValuesOfType = "any prop 1" | "any prop 2" | "any prop 3" // ...
// We can't pass single provider name like Facebook
// Because it waits a complete object of the complexDataType
declare function share(provider: complexDataType):void;
@cenkce
cenkce / types.ts
Last active February 12, 2020 15:48
const shareLinks: {
socialMedia: {
twitter: "twitter",
facebook: "facebook"
},
email: "email",
anotherWay: "anotherWay"
}
// or more complex types
@cenkce
cenkce / find.js
Last active September 30, 2019 10:20
Find uncommon values between two arrays
const firstArray = ['a', 'b', 'c', 'd', 'e'];
const secondArray = ['a', 'b', 'c', 'x', 'a', 'k', 'x','a','r'];
const acc = {};
const table = {};
function explore(val){
if(val !== undefined && !acc[val]){
table[val] = true;
} else if(val !== undefined) {