GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||
GeezerVison is showing github.com — Allow deriving from object and intersection types by ahejlsberg · Pull Request #13604 · microsoft/TypeScript Conversation
|
|
|
||
| // A valid base type is any non-generic object type or intersection of non-generic | ||
| // object types. | ||
| function isValidBaseType(type: Type): boolean { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Return type could be : type is BaseType ?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Well, BaseType is the most restrictive type we can express for a valid base type, but there are still BaseType instances that aren't valid base types (e.g. intersections containing type parameters). So, wouldn't be correct in the negative sense.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
In the second block of examples, are N1 and N2 missing |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
jwbay Yes, thanks. Now fixed. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Please add some tests for the changes in the apparent type for this in intersection types.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Anders Hejlsberg (@ahejlsberg) I'm not 100% sure but it seems like this change breaks a common pattern I use (and probably others, at least in the future) with React. This worked in yesterday's nightly build: export class MyComponent extends React.Component<{ someValue: number }, {}> { shouldComponentUpdate(nextProps: this["props"], nextState: this["state"]) { if (nextProps.someValue > 0) { // ... } } } But now errors: The problem might be related to the fact that { children?: React.ReactNode; } & { someValue: number; } I've tried to work around this by using export class MyComponent extends React.Component<{ someValue: number }, {}> { shouldComponentUpdate(nextProps: typeof this.props, nextState: typeof this.state) { if (nextProps.someValue > 0) { // ... } } } // Error on 'typeof this.props': Identifier expected The relevant ambient declaration for // Base component for plain JS classes class Component<P, S> implements ComponentLifecycle<P, S> { constructor(props?: P, context?: any); constructor(...args: any[]); // ... props: { children?: ReactNode } & P; state: S; // ... } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Rotem Dan (@rotemdan) Yes, there was a minor typo that's causing this issue. I will have a fix shortly. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Thanks, I temporarily worked around these errors by creating a type alias for |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Anders Hejlsberg (@ahejlsberg) this is just CRAZY! I can now build a decorator, export it as const and export a type with the same name. export class Class implements MyDecorator<'myMethod'> { @MyDecorator() myMethod() { // myMethod's signature is fixed, if changed -> type error! if name changes -> type error } } This is getting to be the best type system ever, I am really amazed! thank you guys! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
It would be very nice if a class decorator could "replace" the original class type with respect to the type system so that we could use such a decorator to fully implement a mixin, both the implementation at runtime and the extended type for compile time. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Forgive me if it's a silly question, but would this change help at all with what I'm looking for here? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Brand new to TS here. Is there a simple way to make this work? function Foo() {} class Bar extends Foo { constructor() { super() console.log('Bar!') } } new Bar |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
A class needs to inherit from something that has a "construct signature"; i.e. a declaration of what the shape of the object it returns if called with if this is your function, i would suggest switching it to a class, and switching if this is a function you got from a different module, then consider declaring it as a calss, e.g. you can always cast the type to a constructable type, e.g. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
In reality it is a dynamically generated class as in the following (working) cases: class Foo extends multiple(One, Two, Three) {} class Foo extends MixinOne(MixinTwo(Three)) {} I am relying on those features to do interesting things. These type of things are possible in JS. Can I use interfaces somehow? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Oh, another thing. I know how to declare a type, for example, so that I can extend from a But how do I declare that some function returns a specific class? F.e., // Third party function get(){ return Backbone.View } // My file Class Foo extends get() {} |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Joe Pea (@trusktr) you need #4890, this should land in the next few days. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Mohamed Hegazy (@mhegazy) Once released, how would I write that last example? Is the following correct? interface Constructable<T> { new (...args): T; } // Third party function get(): Constructable<Backbone.View> { return Backbone.View } // My file Class Foo extends get() {} What about the following? function multiple(...classes: whatGoesHere?): Constructable<whatGoesHere?> { // uses Proxy } class Foo extends multiple(One, Two, Three) {} |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Please see #13743 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Does this also allow extending from |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.