טייפּסקריפּט טאַגד מוסטער סטרינגס

ווי צו נוצן מוסטער סטרינגס ווי פאַנגקשאַנז

די באַסיקס: מוסטער סטרינגס

איידער איך רעדן וועגן טאַגד מוסטער סטרינגס, אויך גערופן מוסטער ליטעראַלס, איך נאָר ווילן צו געבן אַ שנעל הקדמה צו מוסטער סטרינגס אין אַלגעמיין. איר קענט פריי צו שפּרינגען צו די ווייַטער קאַפּיטל אויב איר שוין וויסן וואָס דאָס איז וועגן.

מוסטער סטרינגס זענען אַ ספּעציעלע טיפּ פון שטריקל סימבאָלס וואָס קענען אַנטהאַלטן דזשאַוואַסקריפּט אויסדרוקן און שפּאַן איבער קייפל שורות. זיי נוצן די באַקקטיק-אותיות אַנשטאָט פון טאָפּל קוואָטעס, ווייַל עס איז דער פאַל פֿאַר פּראָסט סטרינגס.

וועגן די ספּאַנינג פון אותיות איבער קייפל שורות, אַ פּראָסט שטריקל דאַרף צו אַנטהאַלטן אַ באַקסלאַש אין קאָמבינאַציע מיט די כאַראַקטער "n" צו שאַפֿן אַ נייַע שורה. מיט מוסטער סטרינגס, איר קענען פשוט מאַכן אַ נייַע שורה "ינלינע", אַזוי צו רעדן.

// A common string as reference.
const string = "I'm just a string";

// A template string. For Typescript,
// this value is a plain 'string'-type.
const templateString = `I'm just a string as well`;

די אַוויילאַבילאַטי פון אויסדרוקן אין אַ שטריקל קענען אויך זיין געשווינד דערקלערט. אַנשטאָט נאָר אַלאַוינג שטריקל סימבאָלס, מוסטער סטרינגס אָננעמען אַרביטראַריש אויסדרוקן. די פאלגענדע בייַשפּיל ווייזט וואָס איך מיינען דורך.

function greet(name: string) {
  // Here you see an expression
  // *embedded* inside a string.
  return `Hello, ${name}!`;
}

// "Hello, Tom!"
const greeting = greet("Tom");

// One more example, using
// Typescript's 'rest'-operator
// which allowes any number of values
// and provides them as an array,
// in our case of type 'string'.
function greetAll(...names: string[]) {
  return `Hi everybody: ${names.join(", ")}!`;
}

// "Hi everybody: Tom, Luke!"
const greetingForAll = greetAll("Tom", "Luke");

טאַגד מוסטער סטרינגס

דער גאַנג פון פֿעיִקייטן אַליין וואָלט שוין זיין פייַן, אָבער טעמפּלאַטע סטרינגס קענען אויך זיין געוויינט ווי פאַנגקשאַנז. איר פשוט שטעלן אַ קיווערד אין פראָנט פון די מוסטער שטריקל צו "פאַרבינדן" עס, דערפאר די נאָמען "טאַגד מוסטער סטרינגס". צייט פֿאַר דער ווייַטער בייַשפּיל.

// All previous examples used 'untagged'
// string literals, which means they're 
// practially just a string.
//
// But let's see how we can convert them
// to an acutal function:
function merge(template: TemplateStringsArray, ...params: string[]){
  
  // This needs some explanation:
  //
  // 'template', our first param, contains
  // all strings *inbetween the paramters*,
  // you'll see in a minute what I mean by that.
  //
  // 'params' then is an array of strings
  // that were provided as paramteres to the
  // template string.
  
  // Let's ignore the result for now.
  return "";
}

const what = "test";

// Here's the tagged template string in action.
// The tag is the function name, and like a 
// function the tagged template string can be called.
//
// Let's destruct how this will look like
// in the function defined above.
//
// 'template' = ["Just a", ""];
// 'params'   = ["test"]
const result = merge`Just a ${what}`;

// As you can see, the function splits the string
// into the string-only parts and the expressions.

ווי איר קענען זען, די סינטאַקס קוקט גאַנץ טשיקאַווע און אפֿשר אפילו אַ ביסל פרעמד ווען ערשטער ארבעטן מיט אים. עס זענען מיסטאָמע נישט אַ פּלאַץ פון נוצן קאַסעס וואָס דאַרפן איר צו ינסטרומענט טאַגד מוסטער סטרינגס, אָבער מיר קענען שפּילן מיט אים. אין די פאלגענדע און לעצטע בייַשפּיל, איך'ווע צוזאַמען עטלעכע פאַל שטודיום. ווי איר וועט זען, טאַגד מוסטער סטרינגס קענען געוויינטלעך ווערן גענוצט מיט דזשאַנעריקס און עפענען עטלעכע טשיקאַווע אָפּציעס פֿאַר ימפּלאַמענינג עטלעכע רעקווירעמענץ.

// 
// Generic
//
// Tagged template literals can be generic, too.
function generic<T>(template: TemplateStringsArray, ...params: T[]){
    return template.join(",") + params.join(",")
}

// "A value: ,test"
console.log(generic<string>`A value: ${"test"}`);

//
// Generic (with super powers)
//
// You can specify each type and 
// also limit the number of params!
function coalesce<A, B, C>(template: TemplateStringsArray, ...params: [A, B, C]){
    return template.join(",") + params.join(",")
}

// ", - , - ,value,0,true" 
const res = coalesce<string, number, boolean>`${"value"} - ${0} - ${true}`;

//
// Different return type
//
// Also, tagged literal types don't 
// only have to return strings.
const preview = (template: TemplateStringsArray, ...params: number[]) 
  => (template2: TemplateStringsArray, ...params2: string[])
  => {
    return "what?";
}

// Note the two pairs of backticks
// after each other, we're just calling
// the returned tagged template string
// form our first one!
console.log(preview`${0}``${"a"}`);

איך האָפֿן איר ינדזשויד דעם גאַנץ שנעל פעלד יאַזדע אין איינער פון דזשאַוואַסקריפּט ס ווי געזונט ווי טייפּסקריפּט ס שטריך. מוסטער סטרינגס זענען רובֿ מסתּמא אַ פּראָסט זאַך אין דיין קאָדעבאַסע, טאַגד מוסטער סטרינגס מיסטאָמע נישט, אַזוי עס איז טשיקאַווע צו לערנען מער וועגן אַזאַ נישע פֿעיִקייטן.

פֿאָרשלאָגן

פֿאַרבונדענע

נאָך-וואָרט

שפּראַכן