Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: what is a "naked type parameter"

Tags:

typescript

See https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types

Conditional types in which the checked type is a naked type parameter...

Google doesn't help, or answers are for C#, which I don't know. Couldn't find the term in Typescript docs either. Getting a meaning from context is also hard...

BTW I do know what a "type parameter" is. But what does "naked" mean?

like image 727
Nurbol Alpysbayev Avatar asked Aug 02 '18 10:08

Nurbol Alpysbayev


People also ask

What does ?: Mean in TypeScript?

What does ?: mean in TypeScript? Using a question mark followed by a colon ( ?: ) means a property is optional. That said, a property can either have a value based on the type defined or its value can be undefined .

Can you pass a type as a parameter TypeScript?

This is not possible.

What is the never type in typescript?

The never type is TypeScript's bottom type, the type for values that never occur. So why is the combination of a conditional type and the never type useful? It effectively allows us to remove constituent types from a union type.

What is interfacepersonreadonly in typescript?

interfacePersonReadonly{ readonlyname: string; readonlyage: number; This happens often enough in JavaScript that TypeScript provides a way to create new types based on old types — mapped types. In a mapped type, the new type transforms each property in the old type in the same way.

Why TypeScript with WebGL?

TypeScript with WebGL Helping with JavaScript Quick Fixes Errors Explore how TypeScript extends JavaScript to add more safety and tooling. Primitives Any Literals Union and Intersection Types Unknown and Never Type Primitives Tuples Built-in Utility Types Nullable Types Meta-Types Conditional Types Discriminate Types Indexed Types Mapped Types

How to use predicates in typescript?

A predicate takes the form parameterName is Type, where parameterNamemust be the name of a parameter from the current function signature. Any time isFishis called with some variable, TypeScript will narrowthat variable to that specific type if the original type is compatible. ts // Both calls to 'swim' and 'fly' are now okay.


1 Answers

When they say naked here, they mean that the type parameter is present without being wrapped in another type, (ie, an array, or a tuple, or a function, or a promise or any other generic type)

Ex:

type NakedUsage<T> = T extends boolean ? "YES" : "NO" type WrappedUsage<T> = [T] extends [boolean] ? "YES" : "NO"; // wrapped in a tuple 

The reason naked vs non nakes is important is that naked usages distribute over a union, meaning the conditional type is applied for each member of the union and the result will be the union of all application

type Distributed = NakedUsage<number | boolean > // = NakedUsage<number> | NakedUsage<boolean> =  "NO" | "YES"  type NotDistributed = WrappedUsage<number | boolean > // "NO"     type NotDistributed2 = WrappedUsage<boolean > // "YES" 

Read here about conditional type distribution.

like image 115
Titian Cernicova-Dragomir Avatar answered Oct 02 '22 22:10

Titian Cernicova-Dragomir