2 ways TypeScript LIES to you

preview_player
Показать описание
Recorded live on twitch, GET IN

MY MAIN YT CHANNEL: Has well edited engineering videos

Discord
Рекомендации по теме
Комментарии
Автор

Ah, typescript. It's so... almost good

asdqwe
Автор

You actually can make an array invariant with 4.7 variance annotations

interface InvariantArray<in out T> extends Array<T> {}

ex-xghh
Автор

I've been doing front end with typescript for over 7 years now. What triggers me is that for large scale apps, I feel there is no alternative for quite some time. When I go and write something else like a cli tool, web api etc I always feel REJUVENATED by the plethora of choice. And when I want to write a UI, the programming world right now is like .I. to me.
Edit: typo

georgespanos
Автор

I gave a internal presentation at work about the theorical background behind typescript and the glaring issues it causes. My favorite is creating a never from anything which destroys the type system entirely


class Marker {}
function castSilently<T>(v: Marker): T {
if (v instanceof Marker) throw null;
return v;
}


The argument is reduced to never, but it actually accepts any value, not just instances of Marker

FryuniGamer
Автор

This is what made me go OH MY GOD FINALLY when I tried rust a few years back, immutable actually means immutable and you can't just start pushing items into const arrays.

theaninova
Автор

A weird one to me is that defining an interface/type method using the "method syntax" is not exactly the same as defining it using the "field + function type syntax":

interface Foo { bar(n: number | string): void; } // Using method syntax, parameters are bivariant
const foo: Foo = { bar: (x: string) => { /* ... */ } }
foo.bar(10); // I just passed an integer to a function implementation that only takes a string.

interface Foo2 { bar: (n: number | string) => void; } // Using field + function syntax, parameters are contravariant
const foo2: Foo2 = { bar: (x: string) => { /* ... */ } } // Fails as "it should"

regorsears
Автор

My disappointment at the lack of coconut oil in this video is immeasurable and my day is ruined.

RuffyBankai
Автор

Another example which really hurts my brain; you can return a completely different object from a constructor. Really painful

MrBaudin
Автор

I did a presentation on some of these annoying quirks of TS called "Typescript is a liar.... sometimes" - a play off of one of the greatest 'Its Always Sunny in Philadelphia'. 'Science is a LIAR... sometimes'
My previous work place loved it, my current one is.. well... yeah.
If you make a full length video please do it in the format of the IASiP scene.

michaeljmeyer
Автор

While I think these examples are great, I don’t often (or ever) find myself or the people I work with mutating function arguments and *not returning the result.

The only thing I could think of that might still break is if it’s a nested object and someone makes only a shallow copy, performs a mutation, returns the result, and as a by-product, accidentally mutated the function argument and uses that somewhere else.

Fuzbo_
Автор

If you don't enable "downlevelIteration" in your tsconfig then TS will not let you splat Set objects as if they were arrays. It causes inconsistent behavior esp if you end up calling a JS file that splats a Set because running it with node will work but running it with ts-node will cause the splat to be undefined (if downlevelIteration is not true).

philyoosays
Автор

btw, about "E492: Not an editor command: W". You can bind :W command to :w (and also :WQ and :Wq to :wq). It really hapls me a lot...

tigranrostomyan
Автор

You can also access an element in typed array like this arr[i] - and it does not recognize, that this can return undefined. While it works fine in for loops where you limit the range of i, in other places you can get a runtime error like that.

esper_lvl
Автор

Man typescript at least give you like datatypes like string or number, imagine c where everything is a f*king bytes and the compiler doesn't give a sh*t. In some implementations via pointers you can even MUTATE f*king CONSTANTS

idiota-w-swiecie
Автор

This being the backbone of my everyday stack brings me nightmares

xoutaku
Автор

Enums in TS suck. Sometimes it only accepts the key, sometimes only the value. Sometimes what is typed as a key is really a value at runtime. Not sure if this counts as lying. Sorry can't remember the specifics just thdt this really bit me

MarcelRobitaille
Автор

you can use nominal types, though it will be a bit clunky
type ReadOnlyResult = {
readonly hello : string
} & {description: "readOnly"}

type Result = {
hello : string
}

const toReadOnly = (item) :ReadOnlyResult => item as ReadOnlyResult

const test = {hello:'world'}
const typedTest = toReadOnly(item)

const mutateResult = (item:Result) => {
item.hello = 'barf'
}

// error
mutateResult(typedTest)

serversender
Автор

we need a compilation in the NotNorm macdonald channel: Constant shitting on typescript, but typescript is a good guy. 🤣🤣

mvargasmoran
Автор

To be fair, in the first example its explicitly an (array of [number or string]) not an ([array of number] or [array of string]), so it checks out that adding a number to an array of strings results in an array where the elements are only number or string

Yes, still an easy footgun, but in this case its just setting the wrong type and it doing what's expected of that type rather than doing everything right and encountering unexpected behavior

blucky_yt
Автор

The read-only thing is so strange..
Since js does have a way to make properties read only that you'd think it would transpile to...

thekwoka