Skip to content

Instantly share code, notes, and snippets.

View tswistak's full-sized avatar

Tomasz Świstak tswistak

View GitHub Profile
@tswistak
tswistak / linq.cs
Last active December 17, 2022 16:55
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public static void Main(string[] args)
{
// tworzymy listę tablicową
var list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
@tswistak
tswistak / Hello.tsx
Created May 8, 2019 15:53
InversifyJS with React Hooks, listing 5
import React from 'react';
import { useInjection } from './ioc.react';
import { IProvider } from './providers';
export const Hello: React.FC = () => {
const provider = useInjection<IProvider<string>>('nameProvider');
return (
<h1>Hello {provider.provide()}!</h1>
);
@tswistak
tswistak / ioc.react.tsx
Created May 8, 2019 15:51
InversifyJS with React Hooks, listing 4
import React, { useContext } from 'react';
import { Container, interfaces } from 'inversify';
const InversifyContext = React.createContext<{ container: Container | null }>({ container: null });
type Props = {
container: Container;
};
export const Provider: React.FC<Props> = (props) => {
@tswistak
tswistak / index.tsx
Created May 8, 2019 15:07
InversifyJS with React Hooks, listing 3
import 'reflect-metadata';
import React from 'react';
import ReactDOM from 'react-dom';
import { Hello } from './Hello';
import { container } from './ioc';
import { Provider } from './ioc.react';
const App: React.FC = () => {
return (
<Provider container={container}>
@tswistak
tswistak / ioc.react.tsx
Last active May 17, 2019 10:19
InversifyJS with React Hooks, listing 2
import React from 'react';
import { Container } from 'inversify';
const InversifyContext = React.createContext<{ container: Container | null }>({ container: null });
type Props = {
container: Container;
};
export const Provider: React.FC<Props> = (props) => {
@tswistak
tswistak / Hello.tsx
Created May 7, 2019 20:19
InversifyJS with React Hooks, listing 1
import React from 'react';
import { useInjection } from './ioc.react';
import { IProvider } from './providers';
export const Hello: React.FC = () => {
const provider: IProvider<string>; // here we need to inject our provider
return (
<h1>Hello {provider.provide()}!</h1>
);
@tswistak
tswistak / omit.ts
Created January 16, 2019 10:38
Avoiding any in TypeScript, listing 10
type C = { a: string; b: string; c: boolean; d: string; }
type D = { b: string; c: boolean; }
type ComitKeys = Omit<C, 'c'>;
type ComitD = Omit<C, keyof D>;
const e: ComitKeys = { a: 'b', b: 'b', d: 'd' }; // c doesn't exist
const f: ComitD = { a: 'b', d: 'd' }; // b, c doesn't exist
@tswistak
tswistak / omit.ts
Created January 16, 2019 10:36
Avoiding any in TypeScript, listing 9
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
@tswistak
tswistak / pick.ts
Created January 16, 2019 10:35
Avoiding any in TypeScript, listing 8
type E = { a: string; b: string; }
function withPick<K extends keyof E>(state: Pick<E, K>): void { return; }
function withPartial<E>(state: Partial<E>) { return; }
withPick({ a: null }); // 'null' is not assignable to type string
withPartial({ a: null }); // no error
@tswistak
tswistak / pick.ts
Created January 16, 2019 10:34
Avoiding any in TypeScript, listing 7
type C = { a: string; b: string; c: boolean; d: string; }
type D = { b: string; c: boolean; }
type pickCkeys = Pick<C, 'c'>
type pickCD = Pick<C, keyof D>;
const c: pickCkeys = { c: true }; // a, b, d doesn't exist
const d: pickCD = { b: 'a', c: true }; // a, d doesn't exist