Extending TnsOaProviderType - nativescript

I am trying to implement nativescript-oauth2 with IdentityServer 4 so I am trying to implement a custom provider but I am getting a compilation error as follows:
Property 'providerType' in type 'TnsOaProviderMyProvider' is not
assignable to the same property in base type 'TnsOaProvider'. Type
'"MyProvider"' is not assignable to type 'TnsOaProviderType'
import { TnsOaProvider, TnsOaProviderOptions, OpenIdSupportFull, TnsOaProviderType } from "nativescript-oauth2/providers/providers";
import { ITnsOAuthTokenResult } from "nativescript-oauth2";
export declare type ProviderTypeMyProvider = "MyProvider";
export interface TnsOaProviderOptionsMyProvider extends TnsOaProviderMyProvider {}
export declare class TnsOaProviderMyProvider implements TnsOaProvider {
options: TnsOaProviderOptions;
openIdSupport: OpenIdSupportFull;
providerType: ProviderTypeMyProvider;
authority: string;
tokenEndpointBase: string;
authorizeEndpoint: string;
tokenEndpoint: string;
cookieDomains: string[];
constructor(options: TnsOaProviderOptionsMyProvider);
parseTokenResult(jsonData: any): ITnsOAuthTokenResult;
}
Any idea how to extend the TnsOaProviderType which is defined as follows:
export type TnsOaProviderType =
| ProviderTypeFacebook
| ProviderTypeGoogle
| ProviderTypeMicrosoft
| ProviderTypeLinkedIn;

There seems to be something wrong with your hierarchy.
You are extending TnsOaProviderOptionsMyProvider from TnsOaProviderMyProvider but you want to pass TnsOaProviderOptionsMyProvider as parameter to TnsOaProviderMyProvider itself?
Please double check if you have named them same by mistake.

Related

Is there a way to access an internal parameter in a custom constructor from struct in Go?

I would like to access an internal property in a custom constructor, in my case it's a property from a superclass, like this:
type BaseRepository struct {
database mongo.Database
}
type PointRepository struct {
BaseRepository
pointCollection mongo.Collection
}
func NewPointRepository() *PointRepository {
pointCollection := ***self***.database.GetCollection("points")
pr := &PointRepository{
pointCollection: pointpointCollection,
}
}
As you can see, I need to access something like self to this approach works.
How can I workaround this situation?
There are no constructors or classes in Go.
PointRepository embeds BaseRepository, which has an unexported database field. Any function in the same package as BaseRepository can directly access the database field.
If you need to access that field from a function outside the package, you either have to export it, or you have to provide an exported getter method in BaseRepository.
Solution 1:
Add Set function for BaseRepository
Solution 2:
use unsafe package
type BaseRepository struct {
database string
}
type PointRepository struct {
BaseRepository
pointCollection string
}
baseRepository := &internel.BaseRepository{}
databasePointer := (*string)(unsafe.Pointer(uintptr(unsafe.Pointer(baseRepository))))
*databasePointer = "changed here"
fmt.Printf("%+v",baseRepository)
output:
&{database:changed here}
This is just an example, you should change the type of the field database.

Using ClassNameMap or Record<string,string> type for passing styles from one component to another

I am trying to pass more than one class styles from one component to another.
From Component A I am doing this,
const useStyles = makeStyles(() =>
createStyles({
label:{
width: "100%",
height: "100%",
margin: "10px",
backgroundColor: "white",
},
flex:{
}
})
);
const styles=useStyles();
const getAllList=labelNames.map((name: string):ReactNode => {
return (<Load
label={name}
classes={styles.flex} //here showing error under classes as "Type 'string' is not assignable to type 'ClassNameMap<string> | undefined'."
width={"100%"}
onDataChange={(value: string, data: string): void => {
}}
/>);
});
In Load component I declared like below,
interface Props {
width: string;
label: string;
classes?: ClassNameMap;
onDataChange: (selectedvalue: string, data: string) => void;
}
"Type 'string' is not assignable to type 'ClassNameMap | undefined'."
above error I am getting in the line which I mentioned above. I am unable to find what need to do to fix the error. Any idea or suggestions will help me to resolve. Anyone have any idea about how to pass class styles to specific component.
Also, I need to pass more than one style as props.
ClassNameMap and Record<string, string> are essentially the same. Both are objects whose keys are strings and whose values are string class names. If you want to refine the keys to just the allowed class names, ClassNameMap<‘label’ | ‘flex’> is the same as Record<‘label’ | ‘flex’, string>.
That’s not your issue. Your problem is that the Load component expects an object of class names, but you are providing it with a single string class name styles.flex. That’s why you get the error message:
Type 'string' is not assignable to type 'ClassNameMap | undefined'.
Based on what you are saying here:
I need to pass more than one style as props
Probably you want to pass the entire object that you got from the useStyles hook. Pass classes={styles} instead of classes={styles.flex}.

how do use type-graphql argstype object as a parameter for sequelize findall?

I use nestjs, sequelize-typescript, and type-graphql
I created type-graphql #ArgsType
#ArgsType()
export class TagArgs{
#Field(type => Int)
type: number;
}
and in my resolver, i create query like following using above TagArgs
#Query(returns => [Tag])
tags(#Args() tagArgs: TagArgs): Promise<Tag[]>{
return this.tagService.findAll(tagArgs);
}
following is my findAll function of class TagService
async findAll(tagArgs: TagArgs): Promise<Tag[]>{
return this.tagRepository.findAll<Tag>(tagArgs);
}
But Errors such as TS:2345: Argument of type 'TagArgs' is not assignable to parameter of type 'FindOptions' occur at this.tagRepository.findAll(tagArgs)
how can i use ArgsType object??
Assuming that TagArgs properties have the same signature that Tag entity class, you should do something like that:
this.tagRepository.findAll<Tag>({ where: tagArgs })

Is casting to "keyof T" suppose to work or result in a compile error?

Consider the following TypeScript example:
interface MyInterface{
Prop1: string;
Prop2: number;
Prop3: string;
}
const myVar = "Prop4" as keyof MyInterface;
Running this code in Visual Studio 2017, Visual Studio Code and in Playground successfully compiles (TypeScript 2.9.2); the string value is not type checked against MyInterface but both VS and VSC show the 3 properties of MyInterface as IntelliSense suggestions:
const myVar: keyof MyInterface = "Prop4"; obviously works as intended and throws an error but the first example neither throws an error, nor ensures type safety.
Is this statement legal? If so, how is it suppose to behave? If not, why does it compile?
You are using a type assertion, a type assertion by definition overrides what the compiler knows to be true with what you, as the developer decide is true. If you tell the compiler the string it knows not to be an key of MyInterface is a key of MyInterface it will accept this as it is designed to do (although it will prevent you from asserting between unrelated types, eg this will be an error: let d = true as keyof MyInterface;).
If you want a variable to be typed as the key of an interface but still check that the value assigned to it is valid you can specify the type explicitly as you have.
You could also use a helper function:
interface MyInterface {
Prop1: string;
Prop2: number;
Prop3: string;
}
function keyOf<T>(key: keyof T) {
return key;
}
const myVar = keyOf<MyInterface>("Prop4"); //Argument of type '"Prop4"' is not assignable to parameter of type '"Prop1" | "Prop2" | "Prop3"'.
const myVar2 = keyOf<MyInterface>("Prop3");
Playground link

TypeScript method type signature redundant

Maybe I'm seriously missing something, but I'm unable to get rid of a syntax problem with all my classes.
Here is an example :
class Foo {
bar: (x: string, y: number) => string = (xx: string, yy: number) : string => {
// do some stuff...
};
}
Since I'm enforcing type declarations using tslint, ALL my methods are written like this. It's horrible. Having to copy paste the arguments part, renaming the args names between the type declaration and the lambda declaration is soooo painfull.
So : is there a better way to combine type signature and lambda declaration without all the knee jerking ? I sincerely hope I have missed something and hope this is not "by design" ! :)
You need to configure TSLint to enforce types but ignore the type of the functions:
typedef enforces type definitions to exist. Rule options:
"call-signature" checks return type of functions
"parameter" checks type specifier of function parameters
"property-declaration" checks return types of interface properties
"variable-declaration" checks variable declarations
"member-variable-declaration" checks member variable declarations
You can use a file like this one to configure TSLint. And read this to learn more about how to configure it.
Edit:
If you're targeting ES5, you can also do something like this:
var bar = (x: string, y: number) : string => {
// do some stuff...
};
class Foo {
get bar () { return bar; }
}
var test = (new Foo).bar('hello', 3);
Or:
class Foo {
get bar () {
return (xx: string, yy: number): string => {
// do some stuff...
};
}
}
This way the method's context is preserved and it also exists on the prototype. There's also no need to copy the argument types, TSC will infer them.

Resources