Dev Helpers
← Back

Narrow an unknown string

Narrow an unknown string using TypeScript.

Open this example in the interactive library

Source code

const samples: unknown[] = [null, "Ada", 12, { id: 1 }, [2, 3], true];
console.log(
  samples
    .filter((v): v is string => typeof v === "string")
    .map((v) => v.toUpperCase()),
);

Sample output

[ 'ADA' ]