tsplay.dev | TypeScript Playground Shortener | Blog library
kandi X-RAY | tsplay.dev Summary
kandi X-RAY | tsplay.dev Summary
A link shortener for TypeScript Playground.
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 tsplay.dev
tsplay.dev Key Features
tsplay.dev Examples and Code Snippets
Community Discussions
Trending Discussions on tsplay.dev
QUESTION
I am trying to find a relatively generic way to type POST bodies and the responses I get back in conjunction with their API routes (in a nextjs app).
For this I want the compiler to force me to add a body
type and a return
type to all the API routes, which I achieved with the following interface:
ANSWER
Answered 2022-Apr-12 at 02:01See microsoft/TypeScript#48269 for an authoritative answer to this question.
Numeric keys have always been allowed for string index signatures, because non-symbol
keys in JavaScript are always coerced to strings first. So the "number
" keys should really be more like "numeric strings", but TypeScript allows you to think of them as numbers to support indexing into arrays with numbers.
Prior to TypeScript 2.9, keyof {[k: string]: any}
would have just been string
. But TypeScript 2.9 introduced support for number
and symbol
properties with keyof
. Part of this change is that keyof X
where X
has a string index signature now includes number
. So keyof {[k: string]: any}
is string | number
. This is working as intended.
But for mapped types like Record
, the compiler does not immediately augment the keys this way. Apparently it is important that Record
be properly contravariant in K
(according to the comment in ms/TS#48269 anyway).
But Record
is, after all, equivalent to {[k: string]: any}
, and therefore we have an inconsistency. TypeScript doesn't take consistency as its most important design goal; indeed, it is a non-goal of TypeScript to have a provably correct type system. Productivity is, in some sense, more important than correctness. If fixing an inconsistency would make TypeScript very annoying to use for a lot of people, then it's better to leave the inconsistency. And this is apparently one of those situations; according to the same comment, the inconsistency here can't be eliminated (presumably without destroying some oft-used part of the language, such as numeric keys for arrays), so it stays.
Oh well!
QUESTION
I have the following RxJS code: code example
/* The result is: 4 9 15 4 9 15 */
... why does the second scan
start from the beginning (4,9,15) and does not continues from the previous scan
(displaying 19,24,30). It is the same stream after all?
ANSWER
Answered 2022-Mar-31 at 07:09observables does not work like that. You have to see them as a flow of water, where you can change the water with operators. Every time you do something with numbers$ you are starting a new flow of water. So the first pipe has nothing to do with the other pipe and vice versa.
If you want to have return value about the first scan you have to save the return value of the pipe and extend it with an extra pipe.
QUESTION
Why asignment is not accepted?
...ANSWER
Answered 2022-Mar-03 at 19:04If you want the key1
parameter's type to be either the "partnerData"
or the "deliveryData"
string literal type, then you want it to be the union of those types, which is expressed via the |
operator:
QUESTION
I'm having trouble understanding the mechanism behind a typescript error I'm getting - I'm convinced there's something basic doing a "whoosh" sound as it flies over my head.
I've only found this question on the topic, but while the error looks the same, the setup seems quite different to me.
This is the definition I'm referencing, cut for brevity.
...ANSWER
Answered 2022-Mar-01 at 19:51There is a difference between (T | U)[]
and T[] | U[]
. In the first, each array element can be of type T or U. In the second (which corresponds with your case) the array can only have elements of one of either type.
The question you have to ask yourself is this: How can TypeScript know what elements are allowed when it doesn't know which type of an array a variable is?
QUESTION
I'm trying to figure out how to correctly type the parameter applySnapshotFn
which is a function that will always take the arguments snapshot
and spread ...args
.
ANSWER
Answered 2022-Feb-04 at 21:26I've updated your code on the playground to propose you a solution
QUESTION
I have an API client that can be sync or async, depending on the fetcher. Dramatically simplified, it's like this:
...ANSWER
Answered 2022-Feb-03 at 19:17Type parameters stand in for composed types, so as far as I know they can't be generic in the way it looks like you want.
You can, however, make your return type depend on a type argument using conditional types. Here's a very explicit way to do it:
QUESTION
I'm trying to define generic types for domain/aggregate and entities that would enable me to build aggregates that include a number of domains. All in all this would lead to nested value object classes.
In order to simplify API response I would like to provide a toObject() function that would return a somewhat flatten object with strings (or better yet; ValueType).
In the example below I would like order.toObject() to return the type and object as specified in the end.
I would also like to be able to define a recursive/deep generic type for BaseDomain.toObject()
Any ideas? I welcome feedback and thoughts
...ANSWER
Answered 2022-Jan-29 at 01:26It looks FlattenProperties
should be a recursive conditional type, where the base cases are if T
is a primitive type or if T extends IBaseEntity
for some U
. If T extends IBaseDomain
or IBaseList
for some U
then you can recurse into U
. Or if T
is an object type, you can map its properties recursively. Something like this:
QUESTION
I am having trouble with Typescript not enforcing excess property checks in a way that respects co-constraints within a union.
Seems that excess property checks are defeated if any branch of the union allows the field, even if the actual combination of properties is illegal for any specific branch in the union.
Are there workarounds for my case, or is it something that is likely to be fixed eventually in Typescript?
This is not merely theoretical - this poor union branch expansion for excess property checks in typescript allows runtime errors not picked up by the compiler as shown at https://tsplay.dev/m35Aqw - press Run to see the error.
USE CASE
I want a type that adds properties to serve an optimistic concurrency data model - an optional id, or an optional id AND rev like couchdb. Versioning looks like this...
...ANSWER
Answered 2022-Jan-23 at 10:59The only way to enforce that the compiler disallows certain properties is to set them as optional and as never
, like this:
QUESTION
With the following setup:
...ANSWER
Answered 2022-Jan-03 at 13:01A bit simplified example:
QUESTION
I'm working on a factory; I need to eventually add custom methods, hanks to this answer and this answer, we was able to make it work almost as expected.
Almost because it works only with methods without any required arguments; if we try to add a method with at least one required arguments, we get a compile error.
I tried adding a rest argument array both to the declaration of method
argument and M
type (see below) but it helps only when calling the methods.
(this: E & S, ...args: unknonwn[]) => unknown
ANSWER
Answered 2021-Dec-23 at 07:44Please consider this example which represents your use case:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install tsplay.dev
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