-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invalid types are generated and published #24
Comments
I'm suspecting this maybe causing the problem here Line 6 in a49da7e
Since generics for statics is not a thing |
perhaps you could see a creative way to deal with this weird construct instead? I'd really like to not have this because it pushes the generic into the bytes portion of the encoder/decoder interface which is not needed cause it's always just |
Here's the other thing that bothers me about this - I have a TypeScript project in test/ts-use/ that tries to exercise the typing. What is it doing wrong that it's not picking up what you're experiencing? |
How about we just make export interface BlockEncoder<Code extends number> {
name: string
code: Code
encode(data: any): Uint8Array
}
export interface BlockDecoder<Code extends number> {
code: Code
decode(bytes: Uint8Array): any
} The one thing On dag-cbor and dag-json we can handle a subset of |
That was the whole purpose of that beast 🥺 Now it carries information of what is encoded. In practice it should not matter though because it would accept |
I would guess that it's is the problem with how ipjs does things, but I'd have to check. I have this setup in bunch of places where it works. |
I am not sure what problem are you trying to solve here, so it's hard for me to provide meaningful input. Generally though here is why I made those generic over
Exactly! By having
I think it depends on the audience, I would suspect typed camp will be using this feature and they will appreciate all the compile time info they can get. People who do not care about TS are likely not going to care about any of this anyway. |
This may also explain a bit more why I was so keen on the whole |
If the motivation here is to not have to type the interface CID {
asCID: CID
}
type DAGValue =
| boolean
| number
| string
| null
| CID
type DAGArray = DAGNode[]
type DAGObject = {[key:string]: DAGValue|DAGArray|DAGObject}
type DAGNode = DAGValue | DAGObject | DAGArray
interface ByteView<Code extends number, T> extends Uint8Array {
code?: Code
data?:T
}
interface BlockEncoder<Code extends number, Node extends unknown = unknown> {
encode<T extends Node>(node:T):ByteView<Code, T>
}
interface BlockDecoder<Code extends number, Node extends unknown = unknown> {
decode<T extends Node>(bytes:ByteView<Code, T>):T
}
declare var enigma:BlockEncoder<100> & BlockDecoder<100>
declare var cbor:BlockEncoder<112, DAGNode> & BlockDecoder<112, DAGNode>
declare var anything: BlockEncoder<0, any> & BlockDecoder<0, any>
const e1 = enigma.encode({ hello: "world" })
const out = enigma.decode(e1.slice(1))
out.hello /*
^^^ Object is of type 'unknown'.(2571) */
const out2 = enigma.decode(e1)
out2.hello // block view has type information so it knows hello is there
const c1 = cbor.encode({ my: 'suff' })
cbor.decode(c1).my // knows it has my becouse c1 carries info
const c2 = cbor.decode(new Uint8Array())
if (c2) {
if (typeof c2 === 'object') {
c2 // now it knowns it's CID | DAGObject | DAGArray
} else {
c2 // here it knowns it's boolean|string|number
}
}
const c3 = cbor.encode({ items: new Set([1, 2]) })
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
// Types of property 'items' are incompatible.
// Type 'Set<number>' is not assignable to type 'DAGValue | DAGObject | DAGArray | undefined'.
const a = anything.encode({ method() { throw 'Boom!' }})
const a2 = anything.decode(new Uint8Array())
a2.just.like.that.go.nuts() |
We had to disable lib check here nftstorage/nft.storage#56 (comment) because js-dag-cbor seems to have invalid types generated. Specifically:
T
generic type parameter is not declared for whatever reason. It maybe related to microsoft/TypeScript#41258The text was updated successfully, but these errors were encountered: