type-inference | Type Inference Library written in TypeScript
kandi X-RAY | type-inference Summary
kandi X-RAY | type-inference Summary
The basic algorithm is as follows:. During step 4 (substitution), if a polytype is substituted for a type variable more than once each subsequent instance is given a new set of names for the generic type parameters. This is key for the step 5 to be able to correctly assign the forall quantifiers to the right level of nesting.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of type-inference
type-inference Key Features
type-inference Examples and Code Snippets
Community Discussions
Trending Discussions on type-inference
QUESTION
I have a Storage
class:
ANSWER
Answered 2021-Jun-05 at 13:03To make things clearer, the class Child
:
extends Parent
implements Comparable
static > T max(List list)
T
isChild
- fails because
Child
is not aComparable
static > T max(List list)
T
isChild
?
isParent
- it works because
Child implements Comparable
static > T max(List list)
T
isParent
?
isChild
- it works because
Child extends Parent implements Comparable
Here in case 3), finding a valid T
class can be seen as: "find the first super class of Child
that implements a Comparable
of itself".
As in case 1), it cannot be Child
because it is not a Comparable
.
The first (and only) super class of Child
that implements Comparable
of itself is Parent
.
I couldn't understand how updating
List
toList
made the compiler infer the typeParent
.
List
forcesT
to beChild
List
forces?
to beChild
, notT
QUESTION
I am not exactly sure if in TS we follow this naming convention for parameterized types as C++/Java or many other languages (T,U,V,W
).
I saw many times a mixed usage of parameterized types conventions in TS. For example, in the release notes of TS 2.8:
...ANSWER
Answered 2021-Feb-24 at 03:13(From the comments, mostly)
The closest I can imagine answering this in any way which is not just my opinion would be to point to some documentation that describes what the "TypeScript Type Parameter Naming Convention" is and compare it to the Java Generics tutorial document you linked.
If so, the TypeScript design team's official stance on this seems to be "we do not intend to impose any such conventions on others", or "there are no canonical naming conventions in TS". See microsoft/TypeScript#6168 and microsoft/TypeScript#878, specifically this comment:
[I]n general we're not interested in deciding stylistic things for people. Saying that there's One Approved Style for TS goes against our philosophy that we're here to provide types for JS regardless of how you're writing it (within reasonable parameters, of course 😉).
There's also ESLint's naming-convention
rule and TSLint's naming-convention
rule, which make it possible to enforce type parameter naming conventions in a linter-checked code base, but do not seem to do by default. So no convention seems to be official enough to be enforced by default.
For comparison's sake, let's take a look at the relevant section from the Java Generics tutorial document you linked:
Type Parameter Naming ConventionsBy convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.
The most commonly used type parameter names are:
- E - Element (used extensively by the Java Collections Framework)
- K - Key
- N - Number
- T - Type
- V - Value
- S,U,V etc. - 2nd, 3rd, 4th types
You'll see these names used throughout the Java SE API and the rest of this lesson.
Note that the enumerated list above is described as "the most commonly used type parameter names" and not "the only allowable type parameter names"; it is a list of examples and thus descriptive and not prescriptive.
The part that about choosing "single, uppercase letters" is closer to a prescription: uppercase letters tend to distinguish type names from variable names, and single-character type names tend to distinguish type parameters from specific types like class or interfaces. But I get the same sense that this is not so much a decree from on high but an observation about common practices.
So we could presumably stop there and say "there is no official or canonical type parameter naming convention in either TypeScript or Java, and any unofficial such convention is a matter of opinion."
But for the sake of trying to list out what I think the unofficial convention in TypeScript actually is, I'll go on. Keep in mind that it's my opinion and people could plausibly disagree.
I'd say that the Java naming convention laid out above aligns pretty closely with what I would consider the de facto naming convention for TypeScript generic type parameters: use a single uppercase character, either corresponding to either the first letter of what they represent, such as:
T
for "type", the most general and therefore the most commonly used type parameter name;K
for "key", orP
for "property", both of which tend to be constrained byPropertyKey
orkeyof T
orkeyof SomeInterface
orkeyof SomeClass
;V
for "value", most commonly used as a pair withK
for "key";A
for "arguments" andR
for "return", corresponding to the rest parameter list and return type of function signatures respectively, like(...args: A) => R
;N
for "number",S
for "string",B
for "boolean, for type parameters constrained by primitives;
or some sequence of related types such as:
T
,U
,V
,W
, etc., starting withT
for "type" and then walking through the alphabet when needing more types, keeping in mind that you can only get a few this way;A
,B
,C
,D
, etc., starting from the beginning of the alphabet when you expect to use a whole bunch of type parameters and you haven't already used type parameters for something else.
Such conventions are not absolute, and will tend to be bent where necessary to avoid ambiguity or other confusion. If you need more type parameters than can be obtained above without name collisions, it might be desirable to add a character to the name:
T0
,T1
,T2
,T3
, etc., appending numbers to get a sequence of related types;KT
,KU
,KV
: prefixingK
for "keys of"T
,U
, andV
, respectively;
It is farther from what I consider conventional but still common enough to write short UpperCamelCase names to be more descriptive of what the types represent, with the drawback that they could start being confused for specific types and not type parameters:
Key
,Val
,Prop
,Arg
,Ret
,Type
,This
The following is unconventional (remember, my opinion here!) and should be avoided unless there is some overwhelming extenuating reason to do so:
- Long names that look like interface or class names like
InputType
orProperties
; - Prefixing an uppercase
T
on a longer type name likeTNotRecommended
; - Names that begin with a lowercase letter like
t
oru
ormyType
;
QUESTION
I'm trying to understand the following examples from the Typescript advanced types handbook.
Quoting, it says that:
The following example demonstrates how multiple candidates for the same type variable in co-variant positions causes a union type to be inferred:
...ANSWER
Answered 2020-Dec-05 at 15:45Your observation that one of the examples resolves to never
is accurate and you are not missing any compiler settings. In newer versions of TS, intersections of primitive types resolve to never
. If you revert to an older version you will still see string & number
. In newer version you can still see the contravariant position behavior if you use object types:
QUESTION
What is the type (in the sense of type annotations, not type()
) of variables and parameters that come without a type annotation but with an initial value? E.g.,
ANSWER
Answered 2020-Oct-23 at 11:16Concerning the types situation of this block
QUESTION
I can't figure out how to create a type that is a function whose parameters are set but whose return value is generic and should be inferred from the provided function.
This particular use case is that I'm defining a set of selectors on data. All of the selectors have the same state input, and their output will be a property of that state:
...ANSWER
Answered 2020-Oct-14 at 20:41Here's a neat trick you can use. Create a wrapper function that does nothing but restrict and forward the types.
QUESTION
There are two simple methods using vavr's Either.
...ANSWER
Answered 2020-Aug-20 at 05:36The second example seems to 'lose' the type information for the left side when the map(Either::right)
is applied.
Adding some type 'hints' to the map method call should do the trick. So the testReactorEither will look like this:
QUESTION
I've started using the pattern of a generic result type as a wrapper object that includes a return value and information about the operation like whether it succeeded. Here's an example:
...ANSWER
Answered 2020-Aug-13 at 20:25You could get around the bool
issue with just an overload:
QUESTION
In this very interesting use case of auto return value inference(taken from: https://www.geeksforgeeks.org/type-inference-in-c-auto-and-decltype/ ):
...ANSWER
Answered 2020-Jul-25 at 20:33You'll get one function that returns a value of common type of A
and B
. C++ is a statically typed language, so the type of cond ? a : b
should be known at compile time. There are special rules to determine that common type. Informally speaking, it is a type that A
and B
can be implicitly converted to. If no such type exists, you'll get a compilation error.
For example,
QUESTION
I am currently trying to understand how Java infers the type of lambda expressions. I can illustrate with an example:
Writing:
...ANSWER
Answered 2020-Jul-15 at 17:51producer.send
is a method that accepts a record and a Callback
, and Callback
has exactly one abstract method, which accepts a RecordMetadata
and an Exception
. Therefore, if the compiler sees a lambda as the second argument to producer.send
, it must be implementing the method Callback.onCompletion
, and it must have two arguments, with the first a RecordMetadata
and the second an Exception
.
The point being: it's inferred from the type of the method that you're passing the lambda to.
QUESTION
While studying polyvariadic functions in Haskell I stumbled across the following SO questions:
How to create a polyvariadic haskell function?
Haskell, polyvariadic function and type inference
and thought I will give it a try by implementing a function which takes a variable number of strings and concatenates/merges them into a single string:
...ANSWER
Answered 2020-May-31 at 12:17printf
works without type annotation because of type defaulting in GHCi. The same mechanism that allows you to eval show $ 1 + 2
without specifying concrete types.
GHCi tries to evaluate expressions of type IO a
, so you just need to add appropriate instance for MergeStrings
:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install type-inference
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page