-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Record inheritance in Typescript? #166
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
Comments
Hmm.. I'll look into this for you. What you've typed up there seems very reasonable, but sometimes TypeScript requires some convincing. |
Are you able to compile the code above? I'm running TS 1.0.3.0. Wondering if TS requires a an actual |
Ah yes, that example is valid ES6, but not TypeScript. I haven't tried this, but perhaps this might work:
If not, I'll have to look into how to get this working in TypeScript in better form. |
Hey! I'm not sure yet actually and am open to suggestions. |
I'll play with it hopefully around next week and will report |
After looking around a bit, I think adding generics to some of the types like Record would solve at least the typing issues in #341 and #564 . As for inheriting, I'm not entirely sure how that'd work since microsoft/TypeScript#2225 doesn't seem to be worked on. So maybe inheritance for a future version and just make it generic for now? Let me know if you need to add generics tests for the next version |
Is this related to the present inability to type alias, e.g. |
I wonder if it is possible to do this with https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#extending-expressions, microsoft/TypeScript#4910 and intersection types (see #341 (comment)). All of the above is available in nightly ts build. Any ideas? |
Works fine like below, with Typescript 1.6.
|
The subclassing works now (1.6) but the compiler will put off errors about properties not existing if you don't explicitly declare them:
|
Back to not working with 1.7. Argh. |
I spent a long time trying to the same issue, but at the end of the day, I can't answer the most simple question (maybe someone here can help): Why bother using
|
@zhenwenc Such a class would be immutable, but would not have the |
This "workaround" works for me (functionality and code completion wise without errors): import { Record, Map } from 'immutable';
export interface IModel {
a?: number;
b?: number;
}
interface IModelBase extends IModel, Map<string, any> {}
interface ModelBase extends IModel {
new (values: {[key: string]: any}): IModelBase;
getAB?: () => number;
}
export type Model = Record.IRecord<ModelBase>;
const ModelBaseRecord: ModelBase = Record({
a: 1,
b: 2,
});
class ModelRecord extends ModelBaseRecord {
public getAB = () => this.a + this.b;
}
export default ModelRecord; |
@leonuh @abergs I've created this library that has an interface called TypedRecord that extends from Immutable.Map and changes all return types to the generic provided type. |
I found a class to get prop name as string array, then implemented a SetValue Method this has some rough edges. export class NavigableObject<T>{
constructor(private obj: T, private path: string[] = []) { }
To<R>(p: (x: T) => R): NavigableObject<R> {
let propName = this.getPropName(p)
if (propName) {
return new NavigableObject<R>(
p(this.obj),
this.path.concat(propName)
);
} else {
return new NavigableObject<R>(
p(this.obj),
this.path
);
}
}
getPath() {
return this.path;
}
private static propertyRegEx = /\.([^\.;]+);?\s*\}$/;
private getPropName(propertyFunction: Function) {
let value = NavigableObject.propertyRegEx.exec(propertyFunction.toString())
if (value)
return value[1];
}
}
function NavigatableRecordFactory<X>(defaultValues: X, name?: string) {
abstract class NavigatableRecord<P extends NavigatableRecord<P>> extends Record(defaultValues, name) {
SetValue<T>(fn: (x: NavigableObject<P>) => NavigableObject<T>, value: T) {
return this.setIn(fn(new NavigableObject<any>(this)).getPath(), value)
}
}
return NavigatableRecord;
}
interface IUSER {
Name: string;
Age: number;
}
export class USER extends NavigatableRecordFactory<IUSER>({
Name: "Simy the bothless",
Age: 27,
})<USER> implements IUSER {
Name: string;
Age: number;
} and then use it like state.Name // works
state.SetValue(t => t.To(q => q.Name), "test string") // typecheks
state.SetValue(t => t.To(q => q.Name), 123) // error it also works with nested properties somenestedImmutable.SetValue(t =>t.To(q => q.Date).To(q => q.Time), 213213123) but cant get it work without needing to implement method in child class , |
Latest type definitions are helping out quite a bit more, will be released soon |
I created simple typescript lib to create immutable.js record classes with full inheritance support - https://github.com/MrDegriz/immutable-record-class. Maybe it helps to someone. |
fyi I tried with latest release immutable beta rc and no luck
|
Why is this closed. The problem persists. |
I were struggling with converting a model of ours to a Immutable.Map and found Record, which is perfect because we need to implement some functions.
But It doesn't seem to be possible to use the example syntax i Typescript?
Doesn't compile at all, "The property 'Record' does not exist on value of type 'Immutable'."
Any tips?
The text was updated successfully, but these errors were encountered: