What does TypeScript pipe operator mean when used with strings?
Asked Answered
F

2

8

I know about union types, but what does this pipe syntax mean?

let propName: "name" | "age" | "location";

Source: https://blogs.msdn.microsoft.com/typescript/2016/12/07/announcing-typescript-2-1/

Fin answered 5/1, 2017 at 1:22 Comment(0)
F
8

To answer my own question: those are "String Literal Types" as in https://www.typescriptlang.org/docs/handbook/advanced-types.html#string-literal-types

String literal types allow you to specify the exact value a string must have. In practice string literal types combine nicely with union types, type guards, and type aliases. You can use these features together to get enum-like behavior with strings.

Fin answered 5/1, 2017 at 1:26 Comment(1)
There's a better link. I like to think of it as a string-based enum. Also, it's worth noting that the keyof operator takes a type T and returns a string literal type, comprised of T's properties (the expression keyof T is called an indexed type query).Cerotype
D
-7

We call it pipeline operator. The experimental pipeline operator |> (currently at stage 1) allows the creation of chained function calls in a readable manner. Basically, the pipeline operator provides syntactic sugar on a function call with a single argument allowing you to write

'%21' |> decodeURI instead of decodeURI('%21').

Eg: This is how it works

Dexamyl answered 20/10, 2018 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.