How to use typescript-is - 10 common examples

To help you get started, we’ve selected a few typescript-is examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github FreemapSlovakia / freemap-v3-react / src / processors / changesetsProcessor.ts View on Github external
const { data } = await httpRequest({
        getState,
        method: 'GET',
        url: '//api.openstreetmap.org/api/0.6/changesets',
        expectedStatus: [200, 404],
        params: {
          bbox,
          time: fromTime + (toTime0 ? `,${toTime0}` : ''),
          // eslint-disable-next-line
          display_name: state.changesets.authorName,
        },
        cancelActions: [changesetsSetAuthorName, selectFeature, clearMap],
      });

      const xml = new DOMParser().parseFromString(
        assertType<string>(data),
        'text/xml',
      );

      const rawChangesets = xml.getElementsByTagName('changeset');
      const arrayOfrawChangesets = Array.from(rawChangesets);
      const changesetsFromThisRequest = arrayOfrawChangesets
        .map(rawChangeset => {
          const minLat = parseFloat(rawChangeset.getAttribute('min_lat') ?? '');
          const maxLat = parseFloat(rawChangeset.getAttribute('max_lat') ?? '');
          const minLon = parseFloat(rawChangeset.getAttribute('min_lon') ?? '');
          const maxLon = parseFloat(rawChangeset.getAttribute('max_lon') ?? '');

          const descriptionTag = Array.from(
            rawChangeset.getElementsByTagName('tag'),
          ).find(tag => tag.getAttribute('k') === 'comment');
github FreemapSlovakia / freemap-v3-react / src / index.tsx View on Github external
function loadAppState(): void {
  let appState: AppState | undefined;
  const as = storage.getItem('appState');
  if (as) {
    try {
      appState = assertType<AppState>(JSON.parse(as));
    } catch (e) {
      storage.removeItem('appState');
      throw e;
    }
  }

  if (appState) {
    store.dispatch(setAppState(appState));
  }

  store.dispatch(
    l10nSetChosenLanguage(
      appState?.language?.replace(/-.*/, '') ?? null, // fixing wrong saved language because of bug in older version
    ),
  );
}
github FreemapSlovakia / freemap-v3-react / src / processors / trackViewerSetTrackDataProcessor.ts View on Github external
transform: ({ action }) => {
    if (!action.payload.trackGpx) {
      return action;
    }

    // TODO add error handling for failed string-to-gpx and gpx-to-geojson parsing
    const gpxAsXml = new DOMParser().parseFromString(
      action.payload.trackGpx,
      'text/xml',
    );

    const trackGeojson = assertType<FeatureCollection>(toGeoJSON.gpx(gpxAsXml));

    const startPoints: TrackPoint[] = []; // TODO
    const finishPoints: TrackPoint[] = []; // TODO

    for (const feature of trackGeojson.features) {
      if (feature.geometry.type === 'LineString') {
        const lengthInKm = turfLength(feature);
        const coords = feature.geometry.coordinates;
        const startLonlat = coords[0];
        let startTime: Date | undefined;
        let finishTime: Date | undefined;
        const times = assertType<string[] | undefined>(
          feature.properties && feature.properties.coordTimes,
        );
        if (times) {
          startTime = new Date(times[0]);
github woutervh- / typescript-is / examples / src / index.ts View on Github external
item: V;
}

interface Baz<W> {
    buz: W;
}

interface Foo<T, U> extends Bar<U>, Baz<number> {
    type: 'cool';
    secondItem: T;
    thirdItem: Bar<Bar<boolean>>;
}

console.log(is<Foo<Bar<number>, string>>({}));
console.log(is<Foo<Bar<number>, string>>({ item: 'string', buz: 1, type: 'cool', secondItem: { item: 2 }, thirdItem: { item: { item: true } } }));
console.log(is<boolean>(true));
console.log(is<boolean>(false));
github woutervh- / typescript-is / examples / src / index.ts View on Github external
}

interface Baz<W> {
    buz: W;
}

interface Foo<T, U> extends Bar<U>, Baz<number> {
    type: 'cool';
    secondItem: T;
    thirdItem: Bar<Bar<boolean>>;
}

console.log(is<Foo<Bar<number>, string>>({}));
console.log(is<Foo<Bar<number>, string>>({ item: 'string', buz: 1, type: 'cool', secondItem: { item: 2 }, thirdItem: { item: { item: true } } }));
console.log(is<boolean>(true));
console.log(is<boolean>(false));
github woutervh- / typescript-is / examples / src / index.ts View on Github external
interface Bar<V> /*extends Pick<number, keyof number>*/ {
    item: V;
}

interface Baz<W> {
    buz: W;
}

interface Foo<T, U> extends Bar<U>, Baz<number> {
    type: 'cool';
    secondItem: T;
    thirdItem: Bar<Bar<boolean>>;
}

console.log(is<Foo<Bar<number>, string>>({}));
console.log(is<Foo<Bar<number>, string>>({ item: 'string', buz: 1, type: 'cool', secondItem: { item: 2 }, thirdItem: { item: { item: true } } }));
console.log(is<boolean>(true));
console.log(is<boolean>(false));
github FreemapSlovakia / freemap-v3-react / src / processors / galleryFetchUsersProcessor.ts View on Github external
handle: async ({ getState, dispatch }) => {
    const { data } = await httpRequest({
      getState,
      method: 'GET',
      url: '/gallery/picture-users',
      expectedStatus: 200,
    });

    dispatch(gallerySetUsers(assertType<GalleryUser[]>(data)));
  },
};
github FreemapSlovakia / freemap-v3-react / src / processors / authLoginWithFacebookProcessor.ts View on Github external
if (response.status !== 'connected') {
        dispatch(toastsAddError('logIn.logInError2'));
        return;
      }
    }

    const { data } = await httpRequest({
      getState,
      method: 'POST',
      url: `/auth/login-fb`,
      cancelActions: [],
      expectedStatus: 200,
      data: { accessToken: response.authResponse.accessToken },
    });

    const user = assertType<User>(data);

    dispatch(
      toastsAdd({
        collapseKey: 'login',
        messageKey: 'logIn.success',
        style: 'info',
        timeout: 5000,
      }),
    );

    dispatch(authSetUser(user));
  },
};
github FreemapSlovakia / freemap-v3-react / src / coordinatesParser.ts View on Github external
function toLatLon(
  coord1deg: number,
  coord1min: number,
  coord1sec: number,
  card1: string,
  coord2deg: number,
  coord2min: number,
  coord2sec: number,
  card2: string,
): LatLon {
  const latLon = {
    ...toLatLon1(coord1deg, coord1min, coord1sec, card1),
    ...toLatLon1(coord2deg, coord2min, coord2sec, card2),
  };
  return assertType<LatLon>(latLon);
}
github FreemapSlovakia / freemap-v3-react / src / coordinatesParser.ts View on Github external
export function parseCoordinates(coord: string): LatLon {
  const mXml = P_XML.exec(coord);
  if (mXml) {
    return assertType<LatLon>({
      ...toLatLon1(parseFloat(mXml[1].replace(',', '.')), 0.0, 0.0, 'N'),
      ...toLatLon1(parseFloat(mXml[2].replace(',', '.')), 0.0, 0.0, 'E'),
    });
  }

  const sb: string[] = [];
  const list: Array<string | number> = [];

  const P = new RegExp(patterns, 'ig');

  for (let i = 0; i < 100; i += 1) {
    const m = P.exec(coord);
    if (!m) {
      break;
    }

typescript-is

TypeScript transformer that generates run-time type-checks.

MIT
Latest version published 1 year ago

Package Health Score

57 / 100
Full package analysis

Popular typescript-is functions