Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

docs(README.md): note that Flow's Class<T> works with type parameter but TS typeof T doesn't#62

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
jedwards1211 wants to merge1 commit intoniieani:master
base:master
Choose a base branch
Loading
fromjedwards1211:class-utility-type

Conversation

jedwards1211
Copy link
Contributor

@jedwards1211jedwards1211 commentedNov 18, 2019
edited
Loading

No description provided.

@jedwards1211jedwards1211 changed the titlenote that Flow's Class<T> works with type parameter but TS typeof T d…docs(README.md): note that Flow's Class<T> works with type parameter but TS typeof T doesn'tNov 18, 2019
This unfortunately doesn't work with type parameters:
```ts
// error: 'T' only refers to a type, but is being used as a value here.(2693)
function newInstance<T>(c: typeof T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

But you can:

declarefunctionnewInstance<T>(c:new(..._:any)=>T):T;varx=newInstance(classX{y=1;});x.y=2

Copy link
ContributorAuthor

@jedwards1211jedwards1211Nov 18, 2019
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Okay, I could include that, but I bet it has some limitations like not having type information about the static members of the class, right? I'm 95% sureClass<T> in Flow includes the static member types

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Well, It's not clear how you want to use it. Flow has some limitations too, it's not like that in all scenarios you just get thestatics part with this annotation. It's still a generic, (but the last Flow version I've used is 0.85). But, TS is anyway not the best in inference ofstatic properties and even when it's suppose to have the information.

declareclassHi{staticsss():number;iii():string;}varh=newHi();h.constructor.sss();//            ^^^ <- Error in TS - Works in Flow

Copy link
ContributorAuthor

@jedwards1211jedwards1211Nov 21, 2019
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This case works in Flow at least:

declareclassHi{staticsss():number;iii():string;}functionfoo<T:Hi>(cls: Class<T>){cls.sss()// no errorcls.aaa()// Cannot call `cls.aaa` because property `aaa` is missing in statics of `Hi`}

I'm just saying if we include a note aboutnew (..._: any) => T types, we should mention that it definitely doesn't capture static member types, but thatClass<T> can.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

In the moment you add: in the generics it's become pointless. It's not a "real life usage". Because I would probably write it as:

declareclassHi{staticsss():number;iii():string;}functionfoo(cls:typeofHi){cls.sss()// no errorcls.aaa()// Cannot call `cls.aaa` because property `aaa` is missing in statics of `Hi`}

I think that this demo is quite pointless. You better right about the fact that TS is really weak about static fields and you cannot get them throughconstructor, aka:

consth=newHi();h.constructor.sss();// <- Error, doesn't know `sss` exists

ButClass<T> utility isn't that different thantypeof. The fact TS doesn't recognizestatic fields is the real different.
If you wouldn't use: in the demo, only if a function isn't exported it might work and than you wouldn't need any type annotations (this is where Flow shines).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

  • I didn't talk about TS vs Flow. I've gave examples where Flow is better than TS
  • Overtype, wasn't said about comparing to "losing needed type information", was about doing sophisticated types that aren't give any type safety, only use-less meta data
  • I don't saysequelize definitions are over-typed (By the way I guess in TS it would done very differently). I said giving overtype examples for non-real life senecrious isn't good a thing.

The main point, I thinkClass<T> vstypeof T isn't a good example, because it's misleading and unclear to the point, But showing TS can't handlestatic properties or "prototype chain" is way more relevant while Flow does it

Copy link
ContributorAuthor

@jedwards1211jedwards1211Nov 23, 2019
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Okay, I'm just having trouble understanding why you're against my proposed changes. Would you be in agreement with something like

The TypeScript equivalent ofClass<T> istypeof T in cases whereT is a value, andnew (..._: any) => T in cases whereT is a type; however, unlikeClass<T>,new (..._: any) => T doesn't capture static class member type information.

I should research whethertypeof T captures static class member type information either though (though if it doesn't already, I would expecttypeof T to capture static members in the future, whereas I assumenew (..._: any) => T will never capture static members.)
Or can you propose how you would describe the differences in typing classes, including typing static members?

Copy link
ContributorAuthor

@jedwards1211jedwards1211Nov 23, 2019
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Okay for reference with latest typescript playground:

classFoo{staticbar:number=2}constTypeof:typeofFoo=Fooconsta:string=Typeof.barconstNew:(new(..._:any)=>Foo)=Fooconstb:string=New.bar
const a: stringType 'number' is not assignable to type 'string'.(2322)Property 'bar' does not exist on type 'new (..._: any) => Foo'.(2339)

So it's not correct to say that TS can't handle static properties; it certainly does withtypeof, it just can't do so on a generic type, and this is a concrete difference from Flow I think it would benefit people to know.

So I think anything we document about differences in class typing must discuss bothtypeof T andnew (..._: any) => T, the differences between each, and the fact that Flow can capture the class static member types even when theT inClass<T> is a type parameter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

TS don't dealstatics as part of prototype object well.typeof SomeClass will work as much astypeof anyInstanceObject. ButanyInstanceObject.prototype.constructor.* won't work in TS, Only in Flow.

I didn't say I'm against, I'm not a maintainer of this project and don't decide what goes in or not.

I think it's misleading to present it as "Class<T> vstypeof T". It's not the important part for my opinion and quite misleading. Talking about limitation to extractstatic properties from TS comparing to Flow is way more important and accurate

Copy link
ContributorAuthor

@jedwards1211jedwards1211Nov 26, 2019
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It's very important for people migrating from Flow to TS to learn what to use instead ofClass<T>. They need to know that they won't be able to fully replicateClass<T> in some situations.

I get thattypeof T is not the only answer, my TS section needs to mention bothtypeof T andnew (..._: any) => T to provide a complete answer. You may only usenew (..._: any) => T but it's not necessarily sufficient for everyone's purposes.

This unfortunately doesn't work with type parameters:
```ts
// error: 'T' only refers to a type, but is being used as a value here.(2693)
function newInstance<T>(c: typeof T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

But you can:

declarefunctionnewInstance<T>(c:new(..._:any)=>T):T;varx=newInstance(classX{y=1;});x.y=2

@niieani
Copy link
Owner

Thanks@jedwards1211! Since I don't know enough here, let's wait for@oriSomething to give us a hint of where to go.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@oriSomethingoriSomethingoriSomething left review comments

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@jedwards1211@niieani@oriSomething

[8]ページ先頭

©2009-2025 Movatter.jp